Python Matplotlib-停止动画
我已经用matplotlib制作了一个动画,我正试图将它保存到一个文件中。为此,我似乎需要关闭动画的自动重复。否则,matplotlib将尝试渲染永不结束的电影文件 但是如何防止动画循环?我发现动画函数有一个关键字参数,Python Matplotlib-停止动画,python,python-2.7,animation,matplotlib,Python,Python 2.7,Animation,Matplotlib,我已经用matplotlib制作了一个动画,我正试图将它保存到一个文件中。为此,我似乎需要关闭动画的自动重复。否则,matplotlib将尝试渲染永不结束的电影文件 但是如何防止动画循环?我发现动画函数有一个关键字参数,repeat,可以设置为False,但这对我的代码没有明显的影响!那我该怎么办?我在谷歌上搜索了很长时间都没有用 相关代码如下(最后两行是我认为错误的地方)(主要基于): 你做过这个吗?我发现保存动画会生成一个文件,其帧数由FuncAnimation(,frames=number
repeat
,可以设置为False
,但这对我的代码没有明显的影响!那我该怎么办?我在谷歌上搜索了很长时间都没有用
相关代码如下(最后两行是我认为错误的地方)(主要基于):
你做过这个吗?我发现保存动画会生成一个文件,其帧数由FuncAnimation(,frames=numberofframes)设置 如果输出格式为动画GIF,则播放时通常会重复此操作,但文件仅包含指定的帧数
# Set up figure & 3D axis for animation
fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1], projection='3d')
# ax.axis('off')
# choose a different color for each trajectory
colors = plt.cm.jet(np.linspace(0, 1, n_bodies))
# set up lines and points
lines = sum([ax.plot([], [], [], '-', c=c)
for c in colors], [])
pts = sum([ax.plot([], [], [], 'o', c=c)
for c in colors], [])
# prepare the axes limits
xmin_max = (np.min(r[:,:,0])/2, np.max(r[:,:,0])/2)
ymin_max = (np.min(r[:,:,1])/2, np.max(r[:,:,1])/2)
zmin_max = (np.min(r[:,:,2])/2, np.max(r[:,:,2])/2)
ax.set_xlim(xmin_max)
ax.set_ylim(ymin_max)
ax.set_zlim(zmin_max)
# set point-of-view: specified by (altitude degrees, azimuth degrees)
ax.view_init(30, 0)
# initialization function: plot the background of each frame
def init():
for line, pt in zip(lines, pts):
line.set_data([], [])
line.set_3d_properties([])
pt.set_data([], [])
pt.set_3d_properties([])
return lines + pts
# animation function. This will be called sequentially with the frame number
def animate(i):
# we'll step two time-steps per frame. This leads to nice results.
i = (5 * i) % r.shape[1]
for line, pt, ri in zip(lines, pts, r):
# x, y, z = ri[:i].T
x, y, z = ri[i-1].T
line.set_data(x, y)
line.set_3d_properties(z)
pt.set_data(x, y)
# pt.set_data(x[-1:], y[-1:])
pt.set_3d_properties(z)
# pt.set_3d_properties(z[-1:])
ax.legend(['t = %g' % (i/float(n_timesteps))])
#ax.view_init(30, 0.01 *0.3 * i )
fig.canvas.draw()
return lines + pts
# instantiate the animator.
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=n_timesteps, interval=10, blit=True,repeat=False)
anim.save('../fig/animation.mp4', writer = 'mencoder', fps=15)
print 'done!'
plt.show()
ani = animation.FuncAnimation(fig, update, frames=numberofframes, interval=1000/fps)
filename = 'doppler_plot'
ani.save(filename+'.mp4',writer='ffmpeg',fps=fps)
ani.save(filename+'.gif',writer='imagemagick',fps=fps)