matplotlib的pcolormesh在动画过程中不会更新。为什么?

时间:2018-10-02 14:53:52

标签: python animation matplotlib plot

我试图用pcolormesh制作动画,但是我的代码有问题,所以我决定以this post中的答案之一为起点。答案中有太多图,我只需要pcolormesh,所以我删除了不必要的图。令人费解的是,删除情节由于某种原因破坏了动画。我做了一些最小的示例,其中包含pcolormesh和一些文本。只要我不评论文本更新,代码就可以工作,但是我无法弄清楚原因。

我的问题如下:在某些情况下,pcolormesh是否不会更新?如何在不添加其他绘图或文本的情况下修复动画?

注意:我使用的OSX的blit选项似乎有些问题。

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation

y, x = np.meshgrid(np.linspace(-10, 10,100), np.linspace(-10, 10,100))
z = np.sin(x)*np.sin(x)+np.sin(y)*np.sin(y)

fig = plt.figure(figsize=(16, 8),facecolor='white')
ax = plt.subplot()
quad = ax.pcolormesh(x,y,z,shading='gouraud')
ax.set_xlabel('time')
ax.set_ylabel('amplitude')
cb = fig.colorbar(quad,ax=ax)
time_text = ax.text(0.02, 0.95, 'start', transform=ax.transAxes)

def animate(iter):
    z = np.sin(x-iter/(2*np.pi))*np.sin(x-iter/(2*np.pi))+np.sin(y)*np.sin(y)
    time_text.set_text(str(iter)) #comment this line to break the animation
    quad.set_array(z.ravel())

anim = animation.FuncAnimation(fig,animate,frames=100,interval=50,blit=False,repeat=False)
plt.show()

编辑:正如评论中指出的那样,它与后端有关。我更新的问题是:为什么此代码在某些后端失败? pcolormesh的行为不应该与添加的文本无关吗?

0 个答案:

没有答案