
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/event_handling/lasso_demo.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. meta::
        :keywords: codex

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_gallery_event_handling_lasso_demo.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_event_handling_lasso_demo.py:


==========
Lasso Demo
==========

Use a lasso to select a set of points and get the indices of the selected points.
A callback is used to change the color of the selected points.

.. note::
    This example exercises the interactive capabilities of Matplotlib, and this
    will not appear in the static documentation. Please run this code on your
    machine to see the interactivity.

    You can copy and paste individual parts, or download the entire example
    using the link at the bottom of the page.

.. GENERATED FROM PYTHON SOURCE LINES 17-67



.. image-sg:: /gallery/event_handling/images/sphx_glr_lasso_demo_001.png
   :alt: Lasso points using left mouse button
   :srcset: /gallery/event_handling/images/sphx_glr_lasso_demo_001.png, /gallery/event_handling/images/sphx_glr_lasso_demo_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    from matplotlib import colors as mcolors
    from matplotlib import path
    from matplotlib.collections import RegularPolyCollection
    from matplotlib.widgets import Lasso


    class LassoManager:
        def __init__(self, ax, data):
            # The information of whether a point has been selected or not is stored in the
            # collection's array (0 = out, 1 = in), which then gets colormapped to blue
            # (out) and red (in).
            self.collection = RegularPolyCollection(
                6, sizes=(100,), offset_transform=ax.transData,
                offsets=data, array=np.zeros(len(data)),
                clim=(0, 1), cmap=mcolors.ListedColormap(["tab:blue", "tab:red"]))
            ax.add_collection(self.collection)
            canvas = ax.figure.canvas
            canvas.mpl_connect('button_press_event', self.on_press)
            canvas.mpl_connect('button_release_event', self.on_release)

        def callback(self, verts):
            data = self.collection.get_offsets()
            self.collection.set_array(path.Path(verts).contains_points(data))
            canvas = self.collection.figure.canvas
            canvas.draw_idle()
            del self.lasso

        def on_press(self, event):
            canvas = self.collection.figure.canvas
            if event.inaxes is not self.collection.axes or canvas.widgetlock.locked():
                return
            self.lasso = Lasso(event.inaxes, (event.xdata, event.ydata), self.callback)
            canvas.widgetlock(self.lasso)  # acquire a lock on the widget drawing

        def on_release(self, event):
            canvas = self.collection.figure.canvas
            if hasattr(self, 'lasso') and canvas.widgetlock.isowner(self.lasso):
                canvas.widgetlock.release(self.lasso)


    if __name__ == '__main__':
        np.random.seed(19680801)
        ax = plt.figure().add_subplot(
            xlim=(0, 1), ylim=(0, 1), title='Lasso points using left mouse button')
        manager = LassoManager(ax, np.random.rand(100, 2))
        plt.show()


.. _sphx_glr_download_gallery_event_handling_lasso_demo.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: lasso_demo.ipynb <lasso_demo.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: lasso_demo.py <lasso_demo.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: lasso_demo.zip <lasso_demo.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
