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

        :ref:`Go to the end <sphx_glr_download_gallery_event_handling_image_slices_viewer.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_event_handling_image_slices_viewer.py:


============
Scroll event
============

In this example a scroll wheel event is used to scroll through 2D slices of
3D data.

.. 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 17-54



.. image-sg:: /gallery/event_handling/images/sphx_glr_image_slices_viewer_001.png
   :alt: Use scroll wheel to navigate index 0
   :srcset: /gallery/event_handling/images/sphx_glr_image_slices_viewer_001.png, /gallery/event_handling/images/sphx_glr_image_slices_viewer_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np


    class IndexTracker:
        def __init__(self, ax, X):
            self.index = 0
            self.X = X
            self.ax = ax
            self.im = ax.imshow(self.X[:, :, self.index])
            self.update()

        def on_scroll(self, event):
            print(event.button, event.step)
            increment = 1 if event.button == 'up' else -1
            max_index = self.X.shape[-1] - 1
            self.index = np.clip(self.index + increment, 0, max_index)
            self.update()

        def update(self):
            self.im.set_data(self.X[:, :, self.index])
            self.ax.set_title(
                f'Use scroll wheel to navigate\nindex {self.index}')
            self.im.axes.figure.canvas.draw()


    x, y, z = np.ogrid[-10:10:100j, -10:10:100j, 1:10:20j]
    X = np.sin(x * y * z) / (x * y * z)

    fig, ax = plt.subplots()
    # create an IndexTracker and make sure it lives during the whole
    # lifetime of the figure by assigning it to a variable
    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-jupyter

      :download:`Download Jupyter notebook: image_slices_viewer.ipynb <image_slices_viewer.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: image_slices_viewer.py <image_slices_viewer.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: image_slices_viewer.zip <image_slices_viewer.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
