{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Lasso Demo\n\nUse a lasso to select a set of points and get the indices of the selected points.\nA callback is used to change the color of the selected points.\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\nfrom matplotlib import colors as mcolors\nfrom matplotlib import path\nfrom matplotlib.collections import RegularPolyCollection\nfrom matplotlib.widgets import Lasso\n\n\nclass LassoManager:\n    def __init__(self, ax, data):\n        # The information of whether a point has been selected or not is stored in the\n        # collection's array (0 = out, 1 = in), which then gets colormapped to blue\n        # (out) and red (in).\n        self.collection = RegularPolyCollection(\n            6, sizes=(100,), offset_transform=ax.transData,\n            offsets=data, array=np.zeros(len(data)),\n            clim=(0, 1), cmap=mcolors.ListedColormap([\"tab:blue\", \"tab:red\"]))\n        ax.add_collection(self.collection)\n        canvas = ax.figure.canvas\n        canvas.mpl_connect('button_press_event', self.on_press)\n        canvas.mpl_connect('button_release_event', self.on_release)\n\n    def callback(self, verts):\n        data = self.collection.get_offsets()\n        self.collection.set_array(path.Path(verts).contains_points(data))\n        canvas = self.collection.figure.canvas\n        canvas.draw_idle()\n        del self.lasso\n\n    def on_press(self, event):\n        canvas = self.collection.figure.canvas\n        if event.inaxes is not self.collection.axes or canvas.widgetlock.locked():\n            return\n        self.lasso = Lasso(event.inaxes, (event.xdata, event.ydata), self.callback)\n        canvas.widgetlock(self.lasso)  # acquire a lock on the widget drawing\n\n    def on_release(self, event):\n        canvas = self.collection.figure.canvas\n        if hasattr(self, 'lasso') and canvas.widgetlock.isowner(self.lasso):\n            canvas.widgetlock.release(self.lasso)\n\n\nif __name__ == '__main__':\n    np.random.seed(19680801)\n    ax = plt.figure().add_subplot(\n        xlim=(0, 1), ylim=(0, 1), title='Lasso points using left mouse button')\n    manager = LassoManager(ax, np.random.rand(100, 2))\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
}