Updating the X-axis values using Matplotlib animation

To update the X-axis values using Matplotlib animation, we can create dynamic plots where the visible X-axis range changes over time. This technique is useful for revealing data progressively or creating engaging visualizations.

Steps to Update X-axis Values

  • Set the figure size and adjust the padding between and around the subplots
  • Create a figure and a set of subplots
  • Create x and y data points using numpy
  • Plot x and y data points using plot method on axis (ax)
  • Make an animation by repeatedly calling a function animate that sets the X-axis value as per the frame
  • To display the figure, use show() method

Example

Here's how to create an animation that progressively reveals a sine wave by updating the X-axis limits ?

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

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

fig, ax = plt.subplots()
x = np.linspace(0, 15, 100)
y = np.sin(x)

ax.plot(x, y, lw=3)

def animate(frame):
    ax.set_xlim(left=0, right=frame)

ani = animation.FuncAnimation(fig, animate, frames=16, interval=200, repeat=True)

plt.show()

How It Works

The animation function animate(frame) is called repeatedly with different frame values. Each time it's called, ax.set_xlim() updates the visible X-axis range from 0 to the current frame value, creating a progressive reveal effect of the sine wave.

Advanced Example with Multiple Updates

You can also update both X and Y axis limits simultaneously for more complex animations ?

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

fig, ax = plt.subplots(figsize=(8, 6))
x = np.linspace(0, 10, 100)
y = np.cos(x) * np.exp(-x/5)

line, = ax.plot(x, y, 'b-', linewidth=2)

def animate(frame):
    # Update X-axis to show progressive revelation
    current_x = frame * 0.2
    ax.set_xlim(0, current_x)
    
    # Optionally update Y-axis based on visible data
    if current_x > 0:
        visible_indices = x <= current_x
        if np.any(visible_indices):
            y_visible = y[visible_indices]
            ax.set_ylim(np.min(y_visible) - 0.1, np.max(y_visible) + 0.1)

ani = animation.FuncAnimation(fig, animate, frames=50, interval=100, repeat=True)

plt.title('Progressive X-axis Animation')
plt.xlabel('X values')
plt.ylabel('Y values')
plt.grid(True)
plt.show()

Key Parameters

Parameter Description Default
frames Number of animation frames None
interval Delay between frames (ms) 200
repeat Whether to repeat animation True

Conclusion

Updating X-axis values in Matplotlib animations allows you to create dynamic visualizations that reveal data progressively. Use set_xlim() within the animation function to control the visible X-axis range, and adjust frame count and interval for smooth animations.

Updated on: 2026-03-25T23:59:49+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements