{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Span Selector\n\nThe `.SpanSelector` is a mouse widget that enables selecting a range on an\naxis.\n\nHere, an x-range can be selected on the upper axis; a detailed view of the\nselected range is then plotted on the lower axis.\n\n<div class=\"alert alert-info\"><h4>Note</h4><p>If the SpanSelector object is garbage collected you will lose the\n    interactivity.  You must keep a hard reference to it to prevent this.</p></div>\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\n\nfrom matplotlib.widgets import SpanSelector\n\n# Fixing random state for reproducibility\nnp.random.seed(19680801)\n\nfig, (ax1, ax2) = plt.subplots(2, figsize=(8, 6))\n\nx = np.arange(0.0, 5.0, 0.01)\ny = np.sin(2 * np.pi * x) + 0.5 * np.random.randn(len(x))\n\nax1.plot(x, y)\nax1.set_ylim(-2, 2)\nax1.set_title('Press left mouse button and drag '\n              'to select a region in the top graph')\n\nline2, = ax2.plot([], [])\n\n\ndef onselect(xmin, xmax):\n    indmin, indmax = np.searchsorted(x, (xmin, xmax))\n    indmax = min(len(x) - 1, indmax)\n\n    region_x = x[indmin:indmax]\n    region_y = y[indmin:indmax]\n\n    if len(region_x) >= 2:\n        line2.set_data(region_x, region_y)\n        ax2.set_xlim(region_x[0], region_x[-1])\n        ax2.set_ylim(region_y.min(), region_y.max())\n        fig.canvas.draw_idle()\n\n\nspan = SpanSelector(\n    ax1,\n    onselect,\n    \"horizontal\",\n    useblit=True,\n    props=dict(alpha=0.5, facecolor=\"tab:blue\"),\n    interactive=True,\n    drag_from_anywhere=True\n)\n# Set useblit=True on most backends for enhanced performance.\n\n\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. admonition:: References\n\n   The use of the following functions, methods, classes and modules is shown\n   in this example:\n\n   - `matplotlib.widgets.SpanSelector`\n\n"
      ]
    }
  ],
  "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.7"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}