简体   繁体   English

Matplotlib-停止动画

[英]Matplotlib - stop animation

I have made an animation using matplotlib and I'm trying to save it to a file. 我已经使用matplotlib制作了动画,并尝试将其保存到文件中。 For this it seems I would need to turn off automatic repeating of the animation. 为此,我似乎需要关闭动画的自动重复。 If not, matplotlib will try to render a movie file that never ends. 如果没有,则matplotlib将尝试渲染永无休止的电影文件。

But how do I keep the animation from looping? 但是如何防止动画循环播放? I have found that there is a keyword argument for the animation function, repeat , that can be set to False , but this has no apparent effect on my code! 我发现动画函数存在一个关键字参数repeat ,可以将其设置为False ,但这对我的代码没有明显影响! So what should I do? 所以我该怎么做? I've been googling for way to long with no avail. 我已经搜寻了很长时间,但无济于事。

The relevant code is as follows (last two lines is where I believe the error must be) (largely based on 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()

Have you run this? 你跑了吗? I have found that saving the animation produces a file with the number of frames set by FuncAnimation( , ,frames=numberofframes). 我发现保存动画会生成一个文件,该文件具有由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)

If the output format is an animated GIF, this will usually repeat when played, but the file will only contain the number of frames specified. 如果输出格式是GIF动画,则播放时通常会重复此过程,但是文件仅包含指定的帧数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM