.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/event_handling/legend_picking.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. meta:: :keywords: codex .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_event_handling_legend_picking.py: ============== Legend Picking ============== Enable picking on the legend to toggle the original line on and off .. 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 16-52 .. image-sg:: /gallery/event_handling/images/sphx_glr_legend_picking_001.png :alt: Click on legend line to toggle line on/off :srcset: /gallery/event_handling/images/sphx_glr_legend_picking_001.png, /gallery/event_handling/images/sphx_glr_legend_picking_001_2_0x.png 2.0x :class: sphx-glr-single-img .. code-block:: default import numpy as np import matplotlib.pyplot as plt t = np.linspace(0, 1) y1 = 2 * np.sin(2*np.pi*t) y2 = 4 * np.sin(2*np.pi*2*t) fig, ax = plt.subplots() ax.set_title('Click on legend line to toggle line on/off') line1, = ax.plot(t, y1, lw=2, label='1 Hz') line2, = ax.plot(t, y2, lw=2, label='2 Hz') leg = ax.legend(fancybox=True, shadow=True) lines = [line1, line2] lined = {} # Will map legend lines to original lines. for legline, origline in zip(leg.get_lines(), lines): legline.set_picker(True) # Enable picking on the legend line. lined[legline] = origline def on_pick(event): # On the pick event, find the original line corresponding to the legend # proxy line, and toggle its visibility. legline = event.artist origline = lined[legline] visible = not origline.get_visible() origline.set_visible(visible) # Change the alpha on the line in the legend so we can see what lines # have been toggled. legline.set_alpha(1.0 if visible else 0.2) fig.canvas.draw() fig.canvas.mpl_connect('pick_event', on_pick) plt.show() .. _sphx_glr_download_gallery_event_handling_legend_picking.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: legend_picking.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: legend_picking.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_