%matplotlib widget
import matplotlib.pyplot as plt
from mpl_interactions import ioff, panhandler, zoom_factory
# A sample image
image = plt.imread(
"https://github.com/matplotlib/matplotlib/raw/v3.3.0/lib/matplotlib/mpl-data/sample_data/ada.png"
)
Originally based on https://gist.github.com/tacaswell/3144287.
To use just pass the axis object to the zoom factory function. Here I also demonstrate using {func}~matplotlib.pyplot.ioff
as a context manager, so we can control where the figure canvas shows up.
with ioff:
fig, ax = plt.subplots()
ax.imshow(image)
disconnect_zoom = zoom_factory(ax)
display(fig.canvas)
I like being able to pan by clicking with the mouse without having to click the toolbar. Below is an example demonstrating the {class}.panhandler
object. It allows for panning by clicking and dragging even if the toolbar pan widget is not active. You can change which button is used via the button
argument. Make sure to assign the panhandler to a variable otherwise it will be garbage collected and will not work.
pan_handler = panhandler(fig)
display(fig.canvas)