This page was generated from examples/zoom-factory.ipynb. Interactive online version: (Warning: The interactions will be much laggier on Binder than on your computer.) Binder badge.

Zooming and Panning

[ ]:
%matplotlib widget
import matplotlib.pyplot as plt

from mpl_interactions import ioff, panhandler, zoom_factory

Load a sample image

[ ]:
# A sample image
image = plt.imread("https://github.com/matplotlib/matplotlib/raw/v3.3.0/lib/matplotlib/mpl-data/sample_data/ada.png")

enable scroll to zoom

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 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)

Scrolling and panning

I like being able to pan by clicking with the mouse without having to click the toolbar. Below is an example demonstrating using the panhandler object allow scrolling using the middle mouse button though by changing the button attribute you can use any mouse button. You need to make sure to assign the panhanler to a variable otherwise it will be garbage collected and will not work.

[ ]:
pan_handler = panhandler(fig)
display(fig.canvas)
[ ]: