{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Data browser\n\nConnecting data between multiple canvases.\n\nThis example covers how to interact data with multiple canvases. This\nlets you select and highlight a point on one axis, and generating the\ndata of that point on the other axis.\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 numpy as np\n\n\nclass PointBrowser:\n    \"\"\"\n    Click on a point to select and highlight it -- the data that\n    generated the point will be shown in the lower Axes.  Use the 'n'\n    and 'p' keys to browse through the next and previous points\n    \"\"\"\n\n    def __init__(self):\n        self.lastind = 0\n\n        self.text = ax.text(0.05, 0.95, 'selected: none',\n                            transform=ax.transAxes, va='top')\n        self.selected, = ax.plot([xs[0]], [ys[0]], 'o', ms=12, alpha=0.4,\n                                 color='yellow', visible=False)\n\n    def on_press(self, event):\n        if self.lastind is None:\n            return\n        if event.key not in ('n', 'p'):\n            return\n        if event.key == 'n':\n            inc = 1\n        else:\n            inc = -1\n\n        self.lastind += inc\n        self.lastind = np.clip(self.lastind, 0, len(xs) - 1)\n        self.update()\n\n    def on_pick(self, event):\n\n        if event.artist != line:\n            return True\n\n        N = len(event.ind)\n        if not N:\n            return True\n\n        # the click locations\n        x = event.mouseevent.xdata\n        y = event.mouseevent.ydata\n\n        distances = np.hypot(x - xs[event.ind], y - ys[event.ind])\n        indmin = distances.argmin()\n        dataind = event.ind[indmin]\n\n        self.lastind = dataind\n        self.update()\n\n    def update(self):\n        if self.lastind is None:\n            return\n\n        dataind = self.lastind\n\n        ax2.clear()\n        ax2.plot(X[dataind])\n\n        ax2.text(0.05, 0.9, f'mu={xs[dataind]:1.3f}\\nsigma={ys[dataind]:1.3f}',\n                 transform=ax2.transAxes, va='top')\n        ax2.set_ylim(-0.5, 1.5)\n        self.selected.set_visible(True)\n        self.selected.set_data([xs[dataind]], [ys[dataind]])\n\n        self.text.set_text('selected: %d' % dataind)\n        fig.canvas.draw()\n\n\nif __name__ == '__main__':\n    import matplotlib.pyplot as plt\n\n    # Fixing random state for reproducibility\n    np.random.seed(19680801)\n\n    X = np.random.rand(100, 200)\n    xs = np.mean(X, axis=1)\n    ys = np.std(X, axis=1)\n\n    fig, (ax, ax2) = plt.subplots(2, 1)\n    ax.set_title('click on point to plot time series')\n    line, = ax.plot(xs, ys, 'o', picker=True, pickradius=5)\n\n    browser = PointBrowser()\n\n    fig.canvas.mpl_connect('pick_event', browser.on_pick)\n    fig.canvas.mpl_connect('key_press_event', browser.on_press)\n\n    plt.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
}