0
0
MatplotlibHow-ToBeginner ยท 3 min read

How to Create Animation in Matplotlib: Simple Guide

To create animation in matplotlib, use the FuncAnimation class from matplotlib.animation. Define an update function that changes the plot for each frame, then pass it along with the figure and frame count to FuncAnimation to generate the animation.
๐Ÿ“

Syntax

The basic syntax for creating an animation with FuncAnimation is:

  • fig: The matplotlib figure object to animate.
  • func: The update function called for each frame to update the plot.
  • frames: Number of frames or iterable of frame values.
  • init_func: Optional function to initialize the plot.
  • interval: Delay between frames in milliseconds.
  • repeat: Whether the animation repeats after the last frame.
python
from matplotlib.animation import FuncAnimation

ani = FuncAnimation(fig, func, frames=100, init_func=init, interval=50, repeat=True)
๐Ÿ’ป

Example

This example shows a simple sine wave animation that updates the wave over time.

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 to change the y-data of the line
def update(frame):
    line.set_ydata(np.sin(x + frame / 10))
    return line,

ani = FuncAnimation(fig, update, frames=100, interval=50, blit=True)
plt.show()
Output
A window opens showing a smoothly moving sine wave animation.
โš ๏ธ

Common Pitfalls

  • Not returning the updated artists (like lines) from the update function can prevent the animation from displaying properly.
  • For better performance, use blit=True and return updated artists.
  • For interactive environments like Jupyter, use %matplotlib notebook or save the animation to a file.
  • Forgetting to call plt.show() will not display the animation window.
python
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

fig, ax = plt.subplots()
line, = ax.plot([0, 1], [0, 1])

# Wrong update function (does not return anything)
def update_wrong(frame):
    line.set_ydata([frame, frame + 1])

# Correct update function (returns updated line)
def update_right(frame):
    line.set_ydata([frame, frame + 1])
    return line,

ani_wrong = FuncAnimation(fig, update_wrong, frames=10, interval=100)
ani_right = FuncAnimation(fig, update_right, frames=10, interval=100)

plt.show()
Output
Two animations run but only the one with the correct update function updates smoothly.
๐Ÿ“Š

Quick Reference

Remember these key points when creating animations with Matplotlib:

  • Use FuncAnimation with a figure, update function, and frames.
  • Update function must return updated plot elements for blit=True.
  • Set interval to control animation speed.
  • Call plt.show() to display the animation window.
  • Use init_func to set up the plot before animation starts.
โœ…

Key Takeaways

Use FuncAnimation with an update function to create animations in matplotlib.
Always return updated plot elements from the update function when using blit=True.
Set the interval parameter to control the speed of the animation.
Call plt.show() to display the animation window.
Use init_func to initialize the plot before animation starts.