Bendik
Bendik

Reputation: 1167

Matplotlib - stop animation

I have made an animation using matplotlib and I'm trying to save it to a file. 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.

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

Upvotes: 1

Views: 1097

Answers (1)

Flannel
Flannel

Reputation: 11

Have you run this? I have found that saving the animation produces a file with the number of frames set by 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.

Upvotes: 1

Related Questions