0
0
MatplotlibHow-ToBeginner ยท 3 min read

How to Animate Plot in Matplotlib: Simple Guide

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

Syntax

The main syntax to create an animation in Matplotlib is using FuncAnimation:

  • FuncAnimation(fig, func, frames, init_func=None, interval=200, repeat=True)

Where:

  • fig: The Matplotlib figure object to animate.
  • func: The update function called for each frame. It modifies the plot.
  • frames: Number of frames or iterable of frame data.
  • init_func: Optional function to draw a clear frame at start.
  • interval: Delay between frames in milliseconds.
  • repeat: Whether animation repeats after the last frame.
python
from matplotlib.animation import FuncAnimation

anim = FuncAnimation(fig, update_function, frames=100, interval=50, repeat=True)
๐Ÿ’ป

Example

This example shows a simple animated sine wave that moves horizontally over time.

python
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

# Create figure and axis
fig, ax = plt.subplots()
x = np.linspace(0, 2 * np.pi, 200)
line, = ax.plot(x, np.sin(x))

# Update function to animate the sine wave
# It shifts the wave by changing the phase

def update(frame):
    line.set_ydata(np.sin(x + frame / 10))
    return line,

# Create animation object
anim = FuncAnimation(fig, update, frames=100, interval=50, blit=True)

plt.show()
Output
A window opens showing a sine wave moving smoothly from left to right.
โš ๏ธ

Common Pitfalls

  • Forgetting to return the updated artists (like lines) from the update function can stop the animation from working.
  • Not using blit=True can make animations slower but is needed if your update function only changes part of the plot.
  • Calling plt.show() before creating the animation will show a static plot.
  • Not keeping a reference to the animation object (e.g., assigning it to a variable) can cause the animation to not display.
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

def update_right(frame):
    line.set_ydata([frame, frame + 1])
    return line,

anim = FuncAnimation(fig, update_right, frames=10, interval=100)
plt.show()
๐Ÿ“Š

Quick Reference

Remember these tips for smooth Matplotlib animations:

  • Use FuncAnimation with a clear update function.
  • Return updated plot elements from the update function.
  • Use blit=True for better performance when possible.
  • Keep a reference to the animation object to prevent garbage collection.
  • Adjust interval for animation speed control.
โœ…

Key Takeaways

Use FuncAnimation with an update function to animate plots in Matplotlib.
Always return updated plot elements from the update function for the animation to work.
Set blit=True for better performance when only parts of the plot change.
Keep a reference to the animation object to avoid it being garbage collected.
Control animation speed with the interval parameter in FuncAnimation.