【发布时间】:2020-04-24 00:17:10
【问题描述】:
当在FuncAnimation 中使用imshow 艺术家时,第一帧不能被删除。它将始终保持第一帧不变,但其他艺术家的行为正确。我们怎样才能解决这个问题?
代码如下:
from matplotlib import pyplot as plt, animation, cm
def show_imshow_blit_bug(start=0, blit=True):
pic = np.zeros((10, 10), dtype=float)
x = np.arange(0, 2 * np.pi, 0.01)
amp = (x.max() - x.min()) / 2
fig, ax = plt.subplots()
extent = [x.min(), x.max(), -amp, amp]
_cm = cm.jet
picf = pic.flat
picf[0] = 1
picf[-1] = 0.5
imart = ax.imshow(pic,
origin='lower',
extent=extent,
cmap=_cm
)
# imart.set_animated(True) # set or not has no effect
line, = ax.plot(x, np.sin(x)*amp)
line2, = ax.plot(x[[0, -1]], np.array([-amp, amp]), 'r')
line2.set_animated(True) # this make line2 disappear, since updater does not return this artist
line3, = ax.plot(x[[0, -1]], np.array([amp, -amp]), 'C1')
#line3 will be keeped since _animated = False
ax.set_xlim(x.min(), x.max())
ax.set_ylim(-amp * 1.1, amp * 1.1)
fig.tight_layout()
def updater(fn):
print(fn)
np.random.shuffle(picf)
imart.set_data(np.ma.masked_where(pic == 0, pic))
line.set_ydata(np.sin(x + fn / 10.0)*amp)
return (imart, line,) # Both artist will be in blit list
nframe = range(start, 200)
anima = animation.FuncAnimation(fig,
updater,
nframe,
interval=20,
blit=blit,
repeat=False
)
plt.show()
我知道有一个hack solution 可以将init_func 中的艺术家设置为不可见。但这是一个 hack,我不想使用与其余代码不兼容的 init_func。
【问题讨论】:
标签: python matplotlib animation