.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/event_handling/image_slices_viewer.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. meta:: :keywords: codex .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_event_handling_image_slices_viewer.py: =================== Image Slices Viewer =================== Scroll through 2D image slices of a 3D array. .. note:: This example exercises the interactive capabilities of Matplotlib, and this will not appear in the static documentation. Please run this code on your machine to see the interactivity. You can copy and paste individual parts, or download the entire example using the link at the bottom of the page. .. GENERATED FROM PYTHON SOURCE LINES 16-60 .. image-sg:: /gallery/event_handling/images/sphx_glr_image_slices_viewer_001.png :alt: use scroll wheel to navigate images :srcset: /gallery/event_handling/images/sphx_glr_image_slices_viewer_001.png, /gallery/event_handling/images/sphx_glr_image_slices_viewer_001_2_0x.png 2.0x :class: sphx-glr-single-img .. code-block:: default import numpy as np import matplotlib.pyplot as plt # Fixing random state for reproducibility np.random.seed(19680801) class IndexTracker: def __init__(self, ax, X): self.ax = ax ax.set_title('use scroll wheel to navigate images') self.X = X rows, cols, self.slices = X.shape self.ind = self.slices//2 self.im = ax.imshow(self.X[:, :, self.ind]) self.update() def on_scroll(self, event): print("%s %s" % (event.button, event.step)) if event.button == 'up': self.ind = (self.ind + 1) % self.slices else: self.ind = (self.ind - 1) % self.slices self.update() def update(self): self.im.set_data(self.X[:, :, self.ind]) self.ax.set_ylabel('slice %s' % self.ind) self.im.axes.figure.canvas.draw() fig, ax = plt.subplots(1, 1) X = np.random.rand(20, 20, 40) tracker = IndexTracker(ax, X) fig.canvas.mpl_connect('scroll_event', tracker.on_scroll) plt.show() .. _sphx_glr_download_gallery_event_handling_image_slices_viewer.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: image_slices_viewer.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: image_slices_viewer.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_