0
0
Matplotlibdata~5 mins

Interactive animation with widgets in Matplotlib

Choose your learning style9 modes available
Introduction

Interactive animations help you explore data by changing parameters easily. Widgets let you control animations without rewriting code.

You want to see how changing a number affects a graph.
You need to explain data trends step-by-step to friends or colleagues.
You want to explore different scenarios quickly in a plot.
You are teaching or learning and want to interact with visual data.
You want to create a simple dashboard to show data changes.
Syntax
Matplotlib
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
import numpy as np

fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.25)
x = np.linspace(0, 2 * np.pi, 100)
initial_freq = 1
[line], = ax.plot(x, np.sin(initial_freq * x))

axfreq = plt.axes([0.25, 0.1, 0.65, 0.03])
freq_slider = Slider(axfreq, 'Freq', 0.1, 5.0, valinit=initial_freq)

def update(val):
    freq = freq_slider.val
    line.set_ydata(np.sin(freq * x))
    fig.canvas.draw_idle()

freq_slider.on_changed(update)
plt.show()

Use Slider from matplotlib.widgets to add interactive sliders.

Connect slider changes to a function that updates the plot.

Examples
This creates a slider widget with a label and value range.
Matplotlib
from matplotlib.widgets import Slider

# Create slider
slider = Slider(ax, 'Label', min_val, max_val, valinit=initial_value)
This function updates the plot when the slider value changes.
Matplotlib
def update(val):
    # Update plot data here
    line.set_ydata(new_data)
    fig.canvas.draw_idle()

slider.on_changed(update)
Make room for the slider by adjusting subplot layout.
Matplotlib
plt.subplots_adjust(bottom=0.25)
# Adjust space for slider below the plot
Sample Program

This program shows a sine wave plot. You can move the slider below to change the frequency of the sine wave. The plot updates instantly as you move the slider.

Matplotlib
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
import numpy as np

# Create data
x = np.linspace(0, 2 * np.pi, 100)
initial_freq = 1

# Create plot
fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.25)  # leave space for slider
line, = ax.plot(x, np.sin(initial_freq * x))
ax.set_title('Sine Wave Frequency Control')

# Slider axis
axfreq = plt.axes([0.25, 0.1, 0.65, 0.03])

# Create slider
freq_slider = Slider(axfreq, 'Freq', 0.1, 5.0, valinit=initial_freq)

# Update function

def update(val):
    freq = freq_slider.val
    line.set_ydata(np.sin(freq * x))
    fig.canvas.draw_idle()

# Connect slider to update function
freq_slider.on_changed(update)

plt.show()
OutputSuccess
Important Notes

Interactive widgets work best in environments that support GUI, like Jupyter notebooks or Python scripts run locally.

Use plt.subplots_adjust() to make space for widgets below the plot.

Always call fig.canvas.draw_idle() to refresh the plot after updating data.

Summary

Widgets let you control plot parameters interactively.

Use sliders to change values like frequency or amplitude in animations.

Connect slider events to update functions to refresh the plot automatically.