

.. _sphx_glr_gallery_event_handling:

.. _event_handling_examples:

Event handling
==============

Matplotlib supports :ref:`event handling <event_handling>` with
a GUI neutral event model, so you can connect to Matplotlib events without
knowledge of what user interface Matplotlib will ultimately be plugged in to.
This has two advantages: the code you write will be more portable, and
Matplotlib events are aware of things like data coordinate space and which
axes the event occurs in so you don't have to mess with low level
transformation details to go from canvas space to data space.  Object picking
examples are also included.



.. raw:: html

    <div class="sphx-glr-thumbnails">

.. thumbnail-parent-div-open

.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Example to show connecting events that occur when the figure closes.">

.. only:: html

  .. image:: /gallery/event_handling/images/thumb/sphx_glr_close_event_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_event_handling_close_event.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Close event</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="An example of how to interact with the plotting canvas by connecting to move and click events.">

.. only:: html

  .. image:: /gallery/event_handling/images/thumb/sphx_glr_coords_demo_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_event_handling_coords_demo.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Mouse move and click events</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example adds a cross-hair as a data cursor.  The cross-hair is implemented as regular line objects that are updated on mouse move.">

.. only:: html

  .. image:: /gallery/event_handling/images/thumb/sphx_glr_cursor_demo_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_event_handling_cursor_demo.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Cross-hair cursor</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Connecting data between multiple canvases.">

.. only:: html

  .. image:: /gallery/event_handling/images/thumb/sphx_glr_data_browser_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_event_handling_data_browser.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Data browser</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Illustrate the figure and Axes enter and leave events by changing the frame colors on enter and leave.">

.. only:: html

  .. image:: /gallery/event_handling/images/thumb/sphx_glr_figure_axes_enter_leave_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_event_handling_figure_axes_enter_leave.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Figure/Axes enter and leave events</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This provides examples of uses of interactive functions, such as ginput, waitforbuttonpress and manual clabel placement.">

.. only:: html

  .. image:: /gallery/event_handling/images/thumb/sphx_glr_ginput_manual_clabel_sgskip_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_event_handling_ginput_manual_clabel_sgskip.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Interactive functions</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="In this example a scroll wheel event is used to scroll through 2D slices of 3D data.">

.. only:: html

  .. image:: /gallery/event_handling/images/thumb/sphx_glr_image_slices_viewer_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_event_handling_image_slices_viewer.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Scroll event</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Show how to connect to keypress events.">

.. only:: html

  .. image:: /gallery/event_handling/images/thumb/sphx_glr_keypress_demo_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_event_handling_keypress_demo.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Keypress event</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="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.">

.. only:: html

  .. image:: /gallery/event_handling/images/thumb/sphx_glr_lasso_demo_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_event_handling_lasso_demo.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Lasso Demo</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Enable picking on the legend to toggle the original line on and off">

.. only:: html

  .. image:: /gallery/event_handling/images/thumb/sphx_glr_legend_picking_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_event_handling_legend_picking.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Legend picking</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Example using mouse events to simulate a looking glass for inspecting data.">

.. only:: html

  .. image:: /gallery/event_handling/images/thumb/sphx_glr_looking_glass_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_event_handling_looking_glass.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Looking glass</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Sharing events across GUIs.">

.. only:: html

  .. image:: /gallery/event_handling/images/thumb/sphx_glr_path_editor_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_event_handling_path_editor.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Path editor</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="You can enable picking by setting the &quot;picker&quot; property of an artist (for example, a Matplotlib Line2D, Text, Patch, Polygon, AxesImage, etc.)">

.. only:: html

  .. image:: /gallery/event_handling/images/thumb/sphx_glr_pick_event_demo_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_event_handling_pick_event_demo.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Pick event demo</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Compute the mean (mu) and standard deviation (sigma) of 100 data sets and plot mu vs. sigma.  When you click on one of the (mu, sigma) points, plot the raw data from the dataset that generated this point.">

.. only:: html

  .. image:: /gallery/event_handling/images/thumb/sphx_glr_pick_event_demo2_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_event_handling_pick_event_demo2.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Pick event demo 2</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This is an example to show how to build cross-GUI applications using Matplotlib event handling to interact with objects on the canvas.">

.. only:: html

  .. image:: /gallery/event_handling/images/thumb/sphx_glr_poly_editor_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_event_handling_poly_editor.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Polygon editor</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="A Matplotlib based game of Pong illustrating one way to write interactive animations that are easily ported to multiple backends.">

.. only:: html

  .. image:: /gallery/event_handling/images/thumb/sphx_glr_pong_sgskip_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_event_handling_pong_sgskip.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Pong</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Downsampling lowers the sample rate or sample size of a signal. In this tutorial, the signal is downsampled when the plot is adjusted through dragging and zooming.">

.. only:: html

  .. image:: /gallery/event_handling/images/thumb/sphx_glr_resample_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_event_handling_resample.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Resampling Data</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Simple example of using general timer objects. This is used to update the time placed in the title of the figure.">

.. only:: html

  .. image:: /gallery/event_handling/images/thumb/sphx_glr_timers_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_event_handling_timers.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Timers</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Example showing the use of a TriFinder object.  As the mouse is moved over the triangulation, the triangle under the cursor is highlighted and the index of the triangle is displayed in the plot title.">

.. only:: html

  .. image:: /gallery/event_handling/images/thumb/sphx_glr_trifinder_event_demo_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_event_handling_trifinder_event_demo.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Trifinder Event Demo</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Creates two identical panels.  Zooming in on the right panel will show a rectangle in the first panel, denoting the zoomed region.">

.. only:: html

  .. image:: /gallery/event_handling/images/thumb/sphx_glr_viewlims_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_event_handling_viewlims.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Viewlims</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example shows how to connect events in one window, for example, a mouse press, to another figure window.">

.. only:: html

  .. image:: /gallery/event_handling/images/thumb/sphx_glr_zoom_window_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_event_handling_zoom_window.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Zoom modifies other Axes</div>
    </div>


.. thumbnail-parent-div-close

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /gallery/event_handling/close_event
   /gallery/event_handling/coords_demo
   /gallery/event_handling/cursor_demo
   /gallery/event_handling/data_browser
   /gallery/event_handling/figure_axes_enter_leave
   /gallery/event_handling/ginput_manual_clabel_sgskip
   /gallery/event_handling/image_slices_viewer
   /gallery/event_handling/keypress_demo
   /gallery/event_handling/lasso_demo
   /gallery/event_handling/legend_picking
   /gallery/event_handling/looking_glass
   /gallery/event_handling/path_editor
   /gallery/event_handling/pick_event_demo
   /gallery/event_handling/pick_event_demo2
   /gallery/event_handling/poly_editor
   /gallery/event_handling/pong_sgskip
   /gallery/event_handling/resample
   /gallery/event_handling/timers
   /gallery/event_handling/trifinder_event_demo
   /gallery/event_handling/viewlims
   /gallery/event_handling/zoom_window

