

.. _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.




.. image:: /examples/images/sphx_glr_artist_labels_001.png
    :align: center





.. 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="$y = {}x$".format(i))

    # 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()



.. only :: html

 .. container:: sphx-glr-footer


  .. container:: sphx-glr-download

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



  .. container:: sphx-glr-download

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


.. only:: html

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

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