How to Create Interactive Plots with Matplotlib
To create interactive plots in
matplotlib, use the magic command %matplotlib notebook in Jupyter notebooks to enable zooming and panning. You can also use matplotlib.widgets like sliders and buttons to add interactive controls to your plots.Syntax
To enable interactive plots in Jupyter notebooks, use the magic command %matplotlib notebook. Then create plots as usual with matplotlib.pyplot. For adding interactive controls, import widgets like Slider or Button from matplotlib.widgets and connect them to update functions.
%matplotlib notebook: Enables interactive backend in Jupyter.plt.subplots(): Creates figure and axes for plotting.Slider: Adds a slider widget to control plot parameters.Button: Adds a clickable button widget.on_changed(): Connects slider changes to update functions.
python
%matplotlib notebook import matplotlib.pyplot as plt from matplotlib.widgets import Slider, Button fig, ax = plt.subplots() plt.show()
Output
A blank interactive plot window appears with zoom and pan enabled.
Example
This example shows how to create an interactive sine wave plot with a slider to change the frequency dynamically.
python
%matplotlib notebook import numpy as np import matplotlib.pyplot as plt from matplotlib.widgets import Slider # Create the figure and axis fig, ax = plt.subplots() plt.subplots_adjust(bottom=0.25) # Initial frequency freq = 1 # Generate x values x = np.linspace(0, 2 * np.pi, 400) # Plot initial sine wave [line], = ax.plot(x, np.sin(freq * x)) ax.set_title('Interactive Sine Wave') # Add slider axis axfreq = plt.axes([0.25, 0.1, 0.65, 0.03]) slider = Slider(axfreq, 'Freq', 0.1, 5.0, valinit=freq) # Update function def update(val): line.set_ydata(np.sin(slider.val * x)) fig.canvas.draw_idle() slider.on_changed(update) plt.show()
Output
An interactive plot with a sine wave and a slider below it that changes the wave frequency in real time.
Common Pitfalls
- Not enabling the interactive backend with
%matplotlib notebookor%matplotlib widgetin Jupyter, which disables interactivity. - Forgetting to call
fig.canvas.draw_idle()after updating plot data, so changes don't show. - Placing widget axes overlapping the plot area, which hides parts of the plot.
- Using
%matplotlib inlinewhich produces static images without interactivity.
python
# Wrong: Using inline backend disables interactivity %matplotlib inline import matplotlib.pyplot as plt plt.plot([1, 2, 3], [4, 5, 6]) plt.show() # Right: Use notebook backend for interactivity %matplotlib notebook import matplotlib.pyplot as plt plt.plot([1, 2, 3], [4, 5, 6]) plt.show()
Output
First plot is static image; second plot is interactive with zoom and pan.
Quick Reference
| Feature | Usage | Description |
|---|---|---|
| %matplotlib notebook | Run in Jupyter | Enables interactive plots with zoom and pan |
| Slider | matplotlib.widgets.Slider | Adds slider to control plot parameters dynamically |
| Button | matplotlib.widgets.Button | Adds clickable button for interaction |
| fig.canvas.draw_idle() | Call after update | Refreshes plot display after data changes |
| plt.subplots_adjust() | Adjust layout | Prevents widgets from overlapping plot area |
Key Takeaways
Use %matplotlib notebook or %matplotlib widget in Jupyter to enable interactive plots.
Add widgets like Slider and Button from matplotlib.widgets to control plot parameters.
Always call fig.canvas.draw_idle() after updating plot data to refresh the display.
Avoid using %matplotlib inline if you want interactivity, as it produces static images.
Adjust plot layout with plt.subplots_adjust() to make room for widgets.