Skip to content
Advertisement

Previous frames not cleared when saving matplotlib animation

I am making a matplotlib animation in which a quiver arrow moves across the page. This cannot be achieved in the usual way (creating one Quiver object and updating it with each frame of the animation) because although there is a set_UVC method for updating the u, v components, there is no equivalent method for changing the x, y position of the arrows. Therefore, I am creating a new Quiver object for each frame.

This works fine when I do a plt.show() and the animation is drawn on the screen. The arrow moves from left to right across the page, and when one arrow appears the previous one disappears, which is what I want. However, when I save as a gif or mp4 the previous arrows are not cleared, so I end up with a whole line of arrows appearing. How can I fix this?

My code is as follows:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation

n = 21
x = np.linspace(-1.0, 1.0, num=n)
def animate(i):
    q = plt.quiver(x[i:i+1], [0], [1], [0])
    return q,

plt.gca().set_xlim([-1, 1])
anim = matplotlib.animation.FuncAnimation(plt.gcf(), animate, frames=n,
                                          repeat=True, blit=True)

plt.show()
#anim.save('anim.gif', dpi=80, writer='imagemagick')
#anim.save('anim.mp4', dpi=80, writer='ffmpeg')

Advertisement

Answer

The solution is found here, as suggested by Jean-Sébastien above. My code now reads:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation

n = 21
x = np.linspace(-1.0, 1.0, num=n)
q = plt.quiver(x[:1], [0], [1], [0])
def animate(i):
    q.set_offsets([[x[i], 0]])
    return q,

plt.gca().set_xlim([-1, 1])
anim = matplotlib.animation.FuncAnimation(plt.gcf(), animate, frames=n,
                                          repeat=True, blit=True)

plt.show()
#anim.save('anim.gif', dpi=80, writer='imagemagick')
#anim.save('anim.mp4', dpi=80, writer='ffmpeg')
Advertisement