gpt4 book ai didi

python - Matplotlib - 停止动画

转载 作者:太空宇宙 更新时间:2023-11-03 17:09:49 28 4
gpt4 key购买 nike

我使用 matplotlib 制作了一个动画,并尝试将其保存到文件中。为此,我似乎需要关闭动画的自动重复。如果没有,matplotlib 将尝试渲染一个永远不会结束的电影文件。

但是如何防止动画循环呢?我发现动画函数有一个关键字参数 repeat,可以将其设置为 False,但这对我的代码没有明显影响!所以我该怎么做?我已经在谷歌上搜索了很长时间但没有结果。

相关代码如下(最后两行是我认为一定是错误的地方)(很大程度上基于 this ):

# 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()

最佳答案

你运行过这个吗?我发现保存动画会生成一个文件,其帧数由 FuncAnimation( , ,frames=numberofframes) 设置。

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)

如果输出格式是动画 GIF,则播放时通常会重复,但文件将仅包含指定的帧数。

关于python - Matplotlib - 停止动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34210302/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com