我试图在Matplotlib中保存用FuncAnimation类创建的动画。我的动画更复杂,但是当我试图保存给定的简单示例here时,我得到了相同的错误。在import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
pause = False
def simData():
t_max = 10.0
dt = 0.05
x = 0.0
t = 0.0
while t < t_max:
if not pause:
x = np.sin(np.pi*t)
t = t + dt
yield x, t
def onClick(event):
global pause
pause ^= True
def simPoints(simData):
x, t = simData[0], simData[1]
time_text.set_text(time_template%(t))
line.set_data(t, x)
return line, time_text
fig = plt.figure()
ax = fig.add_subplot(111)
line, = ax.plot([], [], 'bo', ms=10) # I'm still not clear on this stucture...
ax.set_ylim(-1, 1)
ax.set_xlim(0, 10)
time_template = 'Time = %.1f s' # prints running simulation time
time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)
fig.canvas.mpl_connect('button_press_event', onClick)
ani = animation.FuncAnimation(fig, simPoints, simData, blit=False, interval=10,
repeat=True)
plt.show()
但是,当我试图通过添加线条来保存动画时
^{pr2}$
最后,只保存前100帧。在
保存动画后,函数重新启动并按预期显示,显示和更新图形200次(或直到t达到t_max,无论我将其设置为什么)。但是保存的电影只包含前100帧。在
暂停功能使它变得棘手。没有它,我可以在FuncAnimation调用中放入frames=200,而不是使用当前frames参数的迭代器/生成器类型函数。但是只要输入frames=200,帧计数似乎就不可暂停了。在
我该怎么解决这个问题?在