How to animate a time-ordered sequence of Matplotlib plots?

To animate a time-ordered sequence of Matplotlib plots, you can use the FuncAnimation class from matplotlib's animation module. This creates smooth animations by repeatedly calling an update function at regular intervals.

Basic Animation Example

Here's a simple example that animates random data over time ?

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

# Set figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create figure and axis
fig, ax = plt.subplots()

# Initialize empty plot
line, = ax.plot([], [], 'b-')
ax.set_xlim(0, 50)
ax.set_ylim(-3, 3)
ax.set_xlabel('Time')
ax.set_ylabel('Value')
ax.set_title('Animated Time Series')

# Animation function
def animate(frame):
    # Generate time-ordered data
    x = np.arange(frame)
    y = np.sin(x * 0.1) + np.random.normal(0, 0.1, frame)
    
    line.set_data(x, y)
    return line,

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

plt.show()

Animating Image Data

For animating 2D image data that changes over time ?

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

# Set up the figure and axis
fig, ax = plt.subplots(figsize=(6, 6))

# Create initial random data
initial_data = np.random.randn(10, 10)
im = ax.imshow(initial_data, cmap='plasma', animated=True)
ax.set_title('Animated Heatmap')

def animate_heatmap(frame):
    # Generate new random data for each frame
    data = np.random.randn(10, 10) + frame * 0.01
    im.set_array(data)
    return [im]

# Create and run animation
anim = FuncAnimation(fig, animate_heatmap, frames=100, interval=50, blit=True, repeat=True)

plt.show()

Multiple Subplot Animation

You can also animate multiple subplots simultaneously ?

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

# Create subplots
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))

# Initialize plots
line1, = ax1.plot([], [], 'r-', label='Sine Wave')
line2, = ax2.plot([], [], 'g-', label='Cosine Wave')

# Set axis properties
for ax in [ax1, ax2]:
    ax.set_xlim(0, 2*np.pi)
    ax.set_ylim(-1.5, 1.5)
    ax.legend()

ax1.set_title('Sine Wave')
ax2.set_title('Cosine Wave')

def animate_subplots(frame):
    # Time-ordered data
    t = np.linspace(0, 2*np.pi, frame + 1)
    
    # Update both plots
    y1 = np.sin(t + frame * 0.1)
    y2 = np.cos(t + frame * 0.1)
    
    line1.set_data(t, y1)
    line2.set_data(t, y2)
    
    return line1, line2

# Create animation
anim = FuncAnimation(fig, animate_subplots, frames=100, interval=100, blit=True)

plt.tight_layout()
plt.show()

Key Parameters

Parameter Description Example Value
frames Number of frames in animation 100
interval Delay between frames (milliseconds) 50
blit Optimize by only redrawing changed parts True
repeat Loop animation when finished True

Conclusion

Use FuncAnimation for smooth, time-ordered animations in Matplotlib. The animate function updates your plot data for each frame, while parameters like interval and frames control timing and duration.

Updated on: 2026-03-25T22:09:38+05:30

787 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements