

.. _sphx_glr_examples_labeled_points.py:


Displaying a custom label for each individual point
===================================================

mpldatacursor's ``point_labels`` functionality can be emulated with an event
handler that sets the annotation text with a label selected from the target
index.




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





.. code-block:: python


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

    labels = ["a", "b", "c", "d", "e"]
    x = np.array([0, 1, 2, 3, 4])

    fig, ax = plt.subplots()
    line, = ax.plot(x, x, "ro")
    mplcursors.cursor(ax).connect(
        "add", lambda sel: sel.annotation.set_text(labels[sel.target.index]))

    plt.show()



.. only :: html

 .. container:: sphx-glr-footer


  .. container:: sphx-glr-download

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



  .. container:: sphx-glr-download

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


.. only:: html

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

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