We're updating the default styles for Matplotlib 2.0

Learn what to expect in the new updates


Previous topic

event_handling example code: lasso_demo.py

Next topic

event_handling example code: looking_glass.py

This Page

event_handling example code: legend_picking.pyΒΆ

[source code]

Enable picking on the legend to toggle the legended line on and off
import numpy as np
import matplotlib.pyplot as plt

t = np.arange(0.0, 0.2, 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, color='red', label='1 HZ')
line2, = ax.plot(t, y2, lw=2, color='blue', label='2 HZ')
leg = ax.legend(loc='upper left', fancybox=True, shadow=True)

# we will set up a dict mapping legend line to orig line, and enable
# picking on the legend line
lines = [line1, line2]
lined = dict()
for legline, origline in zip(leg.get_lines(), lines):
    legline.set_picker(5)  # 5 pts tolerance
    lined[legline] = origline

def onpick(event):
    # on the pick event, find the orig line corresponding to the
    # legend proxy line, and toggle the visibility
    legline = event.artist
    origline = lined[legline]
    vis = not origline.get_visible()
    # Change the alpha on the line in the legend so we can see what lines
    # have been toggled
    if vis:

fig.canvas.mpl_connect('pick_event', onpick)


Keywords: python, matplotlib, pylab, example, codex (see Search examples)