How to save an animation without showing the previous frames in the video?
Question: I would like to save an animation using Python but I get the frames
superposed! I want to get the frames displayed individually. Please here what
I used:

    
    
    import numpy as np
    from matplotlib import pyplot as plt
    from matplotlib import animation
    from numpy import pi, cos, sin
    
    fig = plt.figure()
    plt.axis([-1.5, 1.5,-1.5, 1.5])
    ax = plt.gca()
    ax.set_aspect(1)
    
    N=100
    
    xp = [None] * N
    yp = [None] * N
    
    def init():
        # initialize an empty list of cirlces
        return []
    
    def animate(i):
    
        xp[i]=sin(i*pi/10)
        yp[i]=cos(i*pi/10)
    
        patches = []
    
        patches.append(ax.add_patch( plt.Circle((xp[i],yp[i]),0.02,color='b') ))
        return patches 
    
    anim = animation.FuncAnimation(fig, animate, init_func=init,
                                   frames=N-1, interval=20, blit=True)
    
    anim.save("example.avi")
    plt.show()
    


Answer: There are some things I'm not sure about and it really seems to be that the
axis.plot() behavior and FuncAnimate() behavior are different. However, the
code below works for both.

# Use only one patch (in your case)

The key point from your code is that you are adding a new circle in addition
to the old circles every iteration:

    
    
    patches = []
    patches.append(ax.add_patch( plt.Circle((xp[i],yp[i]),0.02,color='b') ))
    

Even though you clear the patches list, they are still stored in the axis.

Instead, just create one circle and change its position.

# Clear first frame with `init()`

Also, `init()` needs to clear the patch from the base frame.

# Standalone Example

    
    
    from matplotlib import pyplot as plt
    from matplotlib import animation
    from numpy import pi, cos, sin
    
    fig = plt.figure()
    plt.axis([-1.5, 1.5, -1.5, 1.5])
    ax = plt.gca()
    ax.set_aspect(1)
    N = 100
    xp = []
    yp = []
    
    
    # add one patch at the beginning and then change the position
    patch = plt.Circle((0, 0), 0.02, color='b')
    ax.add_patch(patch)
    def init():
        patch.set_visible(False)
        # return what you want to be cleared when axes are reset
        # this actually clears even if patch not returned it so I'm not sure
        # what it really does
        return tuple()
    
    
    def animate(i):
        patch.set_visible(True)  # there is probably a more efficient way to do this
        # just change the position of the patch
        x, y = sin(i*pi/10), cos(i*pi/10)
        patch.center = x, y
        # I left this. I guess you need a history of positions.
        xp.append(x)
        yp.append(y)
        # again return what you want to be cleared after each frame
        # this actually clears even if patch not returned it so I'm not sure
        # what it really does
        return tuple()
    
    
    anim = animation.FuncAnimation(fig, animate, init_func=init,
                                   frames=N-1, interval=20, blit=True)
    # for anyone else, if you get strange errors, make sure you have ffmpeg
    # on your system and its bin folder in your path or use whatever
    # writer you have as: writer=animation.MencoderWriter etc...
    # and then passing it to save(.., writer=writer)
    anim.save('example.mp4')
    plt.show()
    

# Return values???

Regarding the return values of `init()` and `animate()`, It doesn't seem to
matter what is returned. The single patch still gets moved around and drawn
correctly without clearing previous ones.

