【发布时间】:2019-05-31 16:32:25
【问题描述】:
我正在尝试使用 MatPlotLib FuncAnimation 和等高线图制作一个简单的动画。到目前为止,这是我正在尝试的:
# Create Figure and Axes
figure, ax = plot.subplots()
# Draw new contour with new data from u
def animate(i):
a = u[:, :, int(i)]
CS = ax.contourf(x.flatten(), y.flatten(), a, colors=['blue', 'white'], levels=0)
return CS,
# create animation using the animate() function
myAnimation = FuncAnimation(figure, animate, frames=np.arange(0.0, t, t), interval=10, blit=True)
figure.show()
其中 u[:, :, i] 包含我想在轮廓中绘制的新 Z 数据。
这适用于第一帧,但随后我立即从CS = ax.contourf(...) 行中得到错误:AttributeError: 'QuadContourSet' object has no attribute 'get_zorder' 之后的每次迭代。我确实有一个粗略的动画工作,如下所示:
figure, ax = plot.subplots()
for i in range(0, t):
a = u[:, :, i]
CS = ax.contourf(x.flatten(), y.flatten(), a, colors=['blue', 'white'], levels=0)
figure.show()
sleep(0.01)
但是这个实现为每个步骤创建了一个新的图表,因此效率不高,这就是我关注 FuncAnimation 的原因。如果有人知道为什么我的 FuncAnimation 不起作用,或者如果有另一种方法可以在一个图表上为我的所有数据设置动画,我将不胜感激!我也尝试使用 clf()、pause() 和 draw() 将其全部绘制在一张图上,但无济于事!
【问题讨论】:
-
密钥是
return CS.collections,如副本所示。
标签: python matplotlib animation contour