{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Rectangle and ellipse selectors\n\nClick somewhere, move the mouse, and release the mouse button.\n`.RectangleSelector` and `.EllipseSelector` draw a rectangle or an ellipse\nfrom the initial click position to the current mouse position (within the same\naxes) until the button is released.  A connected callback receives the click-\nand release-events.\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 EllipseSelector, RectangleSelector\n\n\ndef select_callback(eclick, erelease):\n    \"\"\"\n    Callback for line selection.\n\n    *eclick* and *erelease* are the press and release events.\n    \"\"\"\n    x1, y1 = eclick.xdata, eclick.ydata\n    x2, y2 = erelease.xdata, erelease.ydata\n    print(f\"({x1:3.2f}, {y1:3.2f}) --> ({x2:3.2f}, {y2:3.2f})\")\n    print(f\"The buttons you used were: {eclick.button} {erelease.button}\")\n\n\ndef toggle_selector(event):\n    print('Key pressed.')\n    if event.key == 't':\n        for selector in selectors:\n            name = type(selector).__name__\n            if selector.active:\n                print(f'{name} deactivated.')\n                selector.set_active(False)\n            else:\n                print(f'{name} activated.')\n                selector.set_active(True)\n\n\nfig = plt.figure(layout='constrained')\naxs = fig.subplots(2)\n\nN = 100000  # If N is large one can see improvement by using blitting.\nx = np.linspace(0, 10, N)\n\nselectors = []\nfor ax, selector_class in zip(axs, [RectangleSelector, EllipseSelector]):\n    ax.plot(x, np.sin(2*np.pi*x))  # plot something\n    ax.set_title(f\"Click and drag to draw a {selector_class.__name__}.\")\n    selectors.append(selector_class(\n        ax, select_callback,\n        useblit=True,\n        button=[1, 3],  # disable middle button\n        minspanx=5, minspany=5,\n        spancoords='pixels',\n        interactive=True))\n    fig.canvas.mpl_connect('key_press_event', toggle_selector)\naxs[0].set_title(\"Press 't' to toggle the selectors on and off.\\n\"\n                 + axs[0].get_title())\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.RectangleSelector`\n   - `matplotlib.widgets.EllipseSelector`\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
}