我正在寻找一种更新动画中轮廓线的方法,该方法不需要每次重新绘制图形。
我发现大多数对此问题的回答都让人回想起ax.contour
,但是由于我的轮廓叠加在另一幅图像上,所以这太慢了。
我发现的唯一看起来很接近回答问题的答案是通过无效链接来回答:使用FuncAnimation在matplotlib中对轮廓图进行动画处理
编辑:这可能是预期的链接。
示例代码:
#!/usr/bin/env python
import matplotlib.pylab as plt
import matplotlib.animation as anim
from matplotlib.colors import LinearSegmentedColormap as lsc
import numpy
#fig = 0; ax = 0; im = 0; co = 0
image_data = numpy.random.random((100,50,50))
contour_data = numpy.random.random((100,50,50))
def init():
global fig, ax, im, co
fig = plt.figure()
ax = plt.axes()
im = ax.imshow(image_data[0,:,:])
co = ax.contour(contour_data[0,:,:])
def func(n):
im.set_data(image_data[n,:,:])
co.set_array(contour_data[n,:,:])
init()
ani = anim.FuncAnimation(fig, func, frames=100)
plt.show()
Run Code Online (Sandbox Code Playgroud)
干杯。
Perhaps you've figured this out by now; unfortunately, it appears that you must re-declare the entire contour/contourf set of artists and remove the old instance at every timestep. Here's some info copied from this link:
set_array() 方法(我认为)只会影响contourf的颜色映射信息,即使如此,似乎也不会更新。您需要做的是制作一个新的等高线图并删除旧的等高线图,特别是当您需要更改基础等高线数据时。这应该像 C.remove() 一样简单,但由于某种原因,它不存在(我将在一分钟内添加它)。因此,您需要执行以下操作:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 2 * np.pi, 0.1)
X,Y = np.meshgrid(x,x)
f1 = np.sin(X) + np.sin(Y)
f2 = np.cos(X) + np.cos(Y)
plt.figure()
C = plt.contourf(f1)
plt.show()
for coll in C.collections:
plt.gca().collections.remove(coll)
C = plt.contourf(f2)
plt.draw()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1350 次 |
最近记录: |