

.. _sphx_glr_examples_paired_highlight.py:


Linked artists
==============

An example of connecting to cursor events: when an artist is selected, also
highlight its "partner".




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





.. code-block:: python


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


    def main():
        fig, axes = plt.subplots(ncols=2)
        num = 5
        xy = np.random.random((num, 2))

        lines = []
        for i in range(num):
            line, = axes[0].plot((i + 1) * np.arange(10))
            lines.append(line)

        points = []
        for x, y in xy:
            point, = axes[1].plot([x], [y], linestyle="none", marker="o")
            points.append(point)

        cursor = mplcursors.cursor(points + lines, highlight=True)
        pairs = dict(zip(points, lines))
        pairs.update(zip(lines, points))

        @cursor.connect("add")
        def on_add(sel):
            sel.extras.append(cursor.add_highlight(pairs[sel.artist]))

        plt.show()


    if __name__ == "__main__":
        main()



.. only :: html

 .. container:: sphx-glr-footer


  .. container:: sphx-glr-download

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



  .. container:: sphx-glr-download

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


.. only:: html

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

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