{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Lasso Selector\n\nInteractively selecting data points with the lasso tool.\n\nThis examples plots a scatter plot. You can then select a few points by drawing\na lasso loop around the points on the graph. To draw, just click\non the graph, hold, and drag it around the points you need to select.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n\nfrom matplotlib.path import Path\nfrom matplotlib.widgets import LassoSelector\n\n\nclass SelectFromCollection:\n \"\"\"\n Select indices from a matplotlib collection using `LassoSelector`.\n\n Selected indices are saved in the `ind` attribute. This tool fades out the\n points that are not part of the selection (i.e., reduces their alpha\n values). If your collection has alpha < 1, this tool will permanently\n alter the alpha values.\n\n Note that this tool selects collection objects based on their *origins*\n (i.e., `offsets`).\n\n Parameters\n ----------\n ax : `~matplotlib.axes.Axes`\n Axes to interact with.\n collection : `matplotlib.collections.Collection` subclass\n Collection you want to select from.\n alpha_other : 0 <= float <= 1\n To highlight a selection, this tool sets all selected points to an\n alpha value of 1 and non-selected points to *alpha_other*.\n \"\"\"\n\n def __init__(self, ax, collection, alpha_other=0.3):\n self.canvas = ax.figure.canvas\n self.collection = collection\n self.alpha_other = alpha_other\n\n self.xys = collection.get_offsets()\n self.Npts = len(self.xys)\n\n # Ensure that we have separate colors for each object\n self.fc = collection.get_facecolors()\n if len(self.fc) == 0:\n raise ValueError('Collection must have a facecolor')\n elif len(self.fc) == 1:\n self.fc = np.tile(self.fc, (self.Npts, 1))\n\n self.lasso = LassoSelector(ax, onselect=self.onselect)\n self.ind = []\n\n def onselect(self, verts):\n path = Path(verts)\n self.ind = np.nonzero(path.contains_points(self.xys))[0]\n self.fc[:, -1] = self.alpha_other\n self.fc[self.ind, -1] = 1\n self.collection.set_facecolors(self.fc)\n self.canvas.draw_idle()\n\n def disconnect(self):\n self.lasso.disconnect_events()\n self.fc[:, -1] = 1\n self.collection.set_facecolors(self.fc)\n self.canvas.draw_idle()\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 data = np.random.rand(100, 2)\n\n subplot_kw = dict(xlim=(0, 1), ylim=(0, 1), autoscale_on=False)\n fig, ax = plt.subplots(subplot_kw=subplot_kw)\n\n pts = ax.scatter(data[:, 0], data[:, 1], s=80)\n selector = SelectFromCollection(ax, pts)\n\n def accept(event):\n if event.key == \"enter\":\n print(\"Selected points:\")\n print(selector.xys[selector.ind])\n selector.disconnect()\n ax.set_title(\"\")\n fig.canvas.draw()\n\n fig.canvas.mpl_connect(\"key_press_event\", accept)\n ax.set_title(\"Press enter to accept selected points.\")\n\n plt.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.LassoSelector`\n - `matplotlib.path.Path`\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.13.2" } }, "nbformat": 4, "nbformat_minor": 0 }