0
0
MatplotlibHow-ToBeginner ยท 4 min read

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 notebook or %matplotlib widget in 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 inline which 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

FeatureUsageDescription
%matplotlib notebookRun in JupyterEnables interactive plots with zoom and pan
Slidermatplotlib.widgets.SliderAdds slider to control plot parameters dynamically
Buttonmatplotlib.widgets.ButtonAdds clickable button for interaction
fig.canvas.draw_idle()Call after updateRefreshes plot display after data changes
plt.subplots_adjust()Adjust layoutPrevents 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.