hm8*_*hm8 8 python animation matplotlib
我正在尝试使用Matplotlib中的FuncAnimation类保存我创建的动画.我的动画更复杂,但是当我尝试保存这里给出的简单示例时,我得到了同样的错误.
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()
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试通过添加行来保存此动画时
ani.save('test.mp4')
Run Code Online (Sandbox Code Playgroud)
最后,只保存前100帧.
保存动画后,该功能将重新启动并按预期显示,显示和更新数字200次(或直到t达到t_max,无论我设置的是什么).但保存的电影仅包含前100帧.
暂停功能使其变得棘手.没有它,我可以将帧= 200放入FuncAnimation调用,而不是使用我目前用于frame参数的迭代器/生成器类型函数.但是通过输入帧= 200,帧数似乎是不可暂停的.
我怎样才能解决这个问题?
tac*_*ell 11
ani = animation.FuncAnimation(fig, simPoints, simData, blit=False, interval=10,
repeat=True, save_count=200)
Run Code Online (Sandbox Code Playgroud)
将解决问题.
在内部,save仅保存固定数量的帧.如果传入固定长度的序列或数字,mpl可以正确地猜测长度.如果你传入(可能是无限的)生成器并且没有传入save_count它,则默认为100.
| 归档时间: |
|
| 查看次数: |
4058 次 |
| 最近记录: |