
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/widgets/rectangle_selector.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_widgets_rectangle_selector.py>`
        to download the full example code.

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

.. _sphx_glr_gallery_widgets_rectangle_selector.py:


===============================
Rectangle and ellipse selectors
===============================

Click somewhere, move the mouse, and release the mouse button.
`.RectangleSelector` and `.EllipseSelector` draw a rectangle or an ellipse
from the initial click position to the current mouse position (within the same
axes) until the button is released.  A connected callback receives the click-
and release-events.

.. GENERATED FROM PYTHON SOURCE LINES 12-66

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    from matplotlib.widgets import EllipseSelector, RectangleSelector


    def select_callback(eclick, erelease):
        """
        Callback for line selection.

        *eclick* and *erelease* are the press and release events.
        """
        x1, y1 = eclick.xdata, eclick.ydata
        x2, y2 = erelease.xdata, erelease.ydata
        print(f"({x1:3.2f}, {y1:3.2f}) --> ({x2:3.2f}, {y2:3.2f})")
        print(f"The buttons you used were: {eclick.button} {erelease.button}")


    def toggle_selector(event):
        print('Key pressed.')
        if event.key == 't':
            for selector in selectors:
                name = type(selector).__name__
                if selector.active:
                    print(f'{name} deactivated.')
                    selector.set_active(False)
                else:
                    print(f'{name} activated.')
                    selector.set_active(True)


    fig = plt.figure(layout='constrained')
    axs = fig.subplots(2)

    N = 100000  # If N is large one can see improvement by using blitting.
    x = np.linspace(0, 10, N)

    selectors = []
    for ax, selector_class in zip(axs, [RectangleSelector, EllipseSelector]):
        ax.plot(x, np.sin(2*np.pi*x))  # plot something
        ax.set_title(f"Click and drag to draw a {selector_class.__name__}.")
        selectors.append(selector_class(
            ax, select_callback,
            useblit=True,
            button=[1, 3],  # disable middle button
            minspanx=5, minspany=5,
            spancoords='pixels',
            interactive=True))
        fig.canvas.mpl_connect('key_press_event', toggle_selector)
    axs[0].set_title("Press 't' to toggle the selectors on and off.\n"
                     + axs[0].get_title())
    plt.show()




.. image-sg:: /gallery/widgets/images/sphx_glr_rectangle_selector_001.png
   :alt: Press 't' to toggle the selectors on and off. Click and drag to draw a RectangleSelector., Click and drag to draw a EllipseSelector.
   :srcset: /gallery/widgets/images/sphx_glr_rectangle_selector_001.png, /gallery/widgets/images/sphx_glr_rectangle_selector_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 67-74

.. admonition:: References

   The use of the following functions, methods, classes and modules is shown
   in this example:

   - `matplotlib.widgets.RectangleSelector`
   - `matplotlib.widgets.EllipseSelector`


.. _sphx_glr_download_gallery_widgets_rectangle_selector.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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