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

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

.. _sphx_glr_gallery_event_handling_trifinder_event_demo.py:


====================
Trifinder Event Demo
====================

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.

.. 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 18-71



.. image-sg:: /gallery/event_handling/images/sphx_glr_trifinder_event_demo_001.png
   :alt: trifinder event demo
   :srcset: /gallery/event_handling/images/sphx_glr_trifinder_event_demo_001.png, /gallery/event_handling/images/sphx_glr_trifinder_event_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.patches import Polygon
    from matplotlib.tri import Triangulation


    def update_polygon(tri):
        if tri == -1:
            points = [0, 0, 0]
        else:
            points = triang.triangles[tri]
        xs = triang.x[points]
        ys = triang.y[points]
        polygon.set_xy(np.column_stack([xs, ys]))


    def on_mouse_move(event):
        if event.inaxes is None:
            tri = -1
        else:
            tri = trifinder(event.xdata, event.ydata)
        update_polygon(tri)
        ax.set_title(f'In triangle {tri}')
        event.canvas.draw()


    # Create a Triangulation.
    n_angles = 16
    n_radii = 5
    min_radius = 0.25
    radii = np.linspace(min_radius, 0.95, n_radii)
    angles = np.linspace(0, 2 * np.pi, n_angles, endpoint=False)
    angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
    angles[:, 1::2] += np.pi / n_angles
    x = (radii*np.cos(angles)).flatten()
    y = (radii*np.sin(angles)).flatten()
    triang = Triangulation(x, y)
    triang.set_mask(np.hypot(x[triang.triangles].mean(axis=1),
                             y[triang.triangles].mean(axis=1))
                    < min_radius)

    # Use the triangulation's default TriFinder object.
    trifinder = triang.get_trifinder()

    # Setup plot and callbacks.
    fig, ax = plt.subplots(subplot_kw={'aspect': 'equal'})
    ax.triplot(triang, 'bo-')
    polygon = Polygon([[0, 0], [0, 0]], facecolor='y')  # dummy data for (xs, ys)
    update_polygon(-1)
    ax.add_patch(polygon)
    fig.canvas.mpl_connect('motion_notify_event', on_mouse_move)
    plt.show()


.. _sphx_glr_download_gallery_event_handling_trifinder_event_demo.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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