{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Scroll event\n\nIn this example a scroll wheel event is used to scroll through 2D slices of\n3D data.\n\n<div class=\"alert alert-info\"><h4>Note</h4><p>This example exercises the interactive capabilities of Matplotlib, and this\n    will not appear in the static documentation. Please run this code on your\n    machine to see the interactivity.\n\n    You can copy and paste individual parts, or download the entire example\n    using the link at the bottom of the page.</p></div>\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\n\n\nclass IndexTracker:\n    def __init__(self, ax, X):\n        self.index = 0\n        self.X = X\n        self.ax = ax\n        self.im = ax.imshow(self.X[:, :, self.index])\n        self.update()\n\n    def on_scroll(self, event):\n        print(event.button, event.step)\n        increment = 1 if event.button == 'up' else -1\n        max_index = self.X.shape[-1] - 1\n        self.index = np.clip(self.index + increment, 0, max_index)\n        self.update()\n\n    def update(self):\n        self.im.set_data(self.X[:, :, self.index])\n        self.ax.set_title(\n            f'Use scroll wheel to navigate\\nindex {self.index}')\n        self.im.axes.figure.canvas.draw()\n\n\nx, y, z = np.ogrid[-10:10:100j, -10:10:100j, 1:10:20j]\nX = np.sin(x * y * z) / (x * y * z)\n\nfig, ax = plt.subplots()\n# create an IndexTracker and make sure it lives during the whole\n# lifetime of the figure by assigning it to a variable\ntracker = IndexTracker(ax, X)\n\nfig.canvas.mpl_connect('scroll_event', tracker.on_scroll)\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.14.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}