0
0
Matplotlibdata~5 mins

Zoom and pan with toolbar in Matplotlib

Choose your learning style9 modes available
Introduction

Zoom and pan let you look closely at parts of a graph. This helps you understand data better by focusing on details.

You want to explore different parts of a plot without redrawing it.
You need to check small changes or trends in a large dataset visually.
You want to interact with a graph during a presentation or analysis.
You want to compare different sections of a plot easily.
You want to make your data visualization interactive for better insights.
Syntax
Matplotlib
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot(data_x, data_y)
plt.show()

The zoom and pan tools are part of the default toolbar in matplotlib figures.

You do not need extra code to enable them; just use plt.show() to open the interactive window.

Examples
This simple plot opens with zoom and pan tools in the toolbar.
Matplotlib
import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 30, 40]

plt.plot(x, y)
plt.show()
Using subplots also shows the toolbar with zoom and pan enabled.
Matplotlib
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([0, 1, 2], [0, 1, 4])
plt.show()
Sample Program

This program plots x squared values. When the plot window opens, you can use the toolbar buttons to zoom in and pan around the graph.

Matplotlib
import matplotlib.pyplot as plt

# Sample data
x = [0, 1, 2, 3, 4, 5]
y = [0, 1, 4, 9, 16, 25]

# Create plot
fig, ax = plt.subplots()
ax.plot(x, y, label='x squared')

# Add labels and title
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_title('Zoom and Pan Example')
ax.legend()

# Show plot with interactive toolbar
plt.show()
OutputSuccess
Important Notes

The zoom button lets you draw a box to zoom into a specific area.

The pan button lets you click and drag to move the view around.

If you use some environments like Jupyter notebooks, interactive toolbar might need special setup.

Summary

Zoom and pan help explore data visually by focusing on parts of a plot.

Matplotlib includes these tools in the default toolbar automatically.

Just call plt.show() to open an interactive window with zoom and pan enabled.