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

.. only:: html

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

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

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

.. _sphx_glr_examples_artist_labels.py:


Display an artist's label instead of x, y coordinates
=====================================================

Use an event handler to change the annotation text.

.. GENERATED FROM PYTHON SOURCE LINES 7-26



.. image-sg:: /examples/images/sphx_glr_artist_labels_001.png
   :alt: Click on a line to display its label
   :srcset: /examples/images/sphx_glr_artist_labels_001.png
   :class: sphx-glr-single-img





.. code-block:: Python


    import numpy as np
    import matplotlib.pyplot as plt
    import mplcursors

    x = np.linspace(0, 10, 100)

    fig, ax = plt.subplots()
    ax.set_title("Click on a line to display its label")

    # Plot a series of lines with increasing slopes.
    for i in range(1, 20):
        ax.plot(x, i * x, label=f"$y = {i}x$")

    # Use a Cursor to interactively display the label for a selected line.
    mplcursors.cursor().connect(
        "add", lambda sel: sel.annotation.set_text(sel.artist.get_label()))

    plt.show()


.. _sphx_glr_download_examples_artist_labels.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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