How to Use FuncAnimation in Matplotlib for Animations
Use
FuncAnimation from matplotlib.animation to create animations by repeatedly calling an update function that modifies the plot. You provide the figure, update function, and number of frames to animate smoothly.Syntax
The basic syntax of FuncAnimation is:
fig: The Matplotlib figure object to animate.func: The update function called for each frame. It changes the plot elements.frames: Number of frames or an iterable of frame values.interval: Delay between frames in milliseconds.repeat: Whether to repeat the animation after the last frame.
python
FuncAnimation(fig, func, frames=None, init_func=None, fargs=None, save_count=None, interval=200, repeat=True, blit=False)
Example
This example shows a simple animation of a sine wave moving horizontally. The update function changes the y-data of the line for each frame.
python
import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation fig, ax = plt.subplots() x = np.linspace(0, 2 * np.pi, 200) line, = ax.plot(x, np.sin(x)) # Update function for animation def update(frame): line.set_ydata(np.sin(x + frame / 10)) # Shift sine wave return line, ani = FuncAnimation(fig, update, frames=100, interval=50, blit=True) plt.show()
Output
A window opens showing a sine wave smoothly moving to the left.
Common Pitfalls
- Not returning the updated artists (plot elements) from the update function can cause the animation not to update properly.
- For better performance, use
blit=Trueand return the updated artists. - For animations in Jupyter notebooks, use
%matplotlib notebookor save the animation to a file. - For infinite animations, set
repeat=True. Otherwise, the animation stops after the last frame.
python
import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation fig, ax = plt.subplots() line, = ax.plot([0, 1], [0, 1]) def update(frame): line.set_ydata([frame, frame + 1]) # Missing return statement here causes no update when blit=True # Wrong: blit=True but update does not return artists ani_wrong = FuncAnimation(fig, update, frames=10, blit=True) # Right: return updated line def update_correct(frame): line.set_ydata([frame, frame + 1]) return line, ani_right = FuncAnimation(fig, update_correct, frames=10, blit=True)
Quick Reference
| Parameter | Description |
|---|---|
| fig | Matplotlib figure object to animate |
| func | Function called at each frame to update plot elements |
| frames | Number of frames or iterable of frame values |
| interval | Delay between frames in milliseconds |
| repeat | Whether to repeat animation after last frame |
| blit | Optimize drawing by only re-drawing changed parts |
Key Takeaways
Use FuncAnimation with a figure and an update function to create animations.
Always return updated plot elements from the update function when using blit=True.
Set interval to control animation speed and repeat to loop the animation.
Use blit=True for better performance but ensure correct return values.
In Jupyter notebooks, use appropriate backends or save animations to files.