diff --git a/examples/widgets/check_buttons.py b/examples/widgets/check_buttons.py index 936e6d96341c..c259490e4a6d 100644 --- a/examples/widgets/check_buttons.py +++ b/examples/widgets/check_buttons.py @@ -8,23 +8,25 @@ s2 = np.sin(6*np.pi*t) fig, ax = plt.subplots() -l0, = ax.plot(t, s0, visible=False, lw=2) -l1, = ax.plot(t, s1, lw=2) -l2, = ax.plot(t, s2, lw=2) +ax.plot(t, s0, visible=False, lw=2, color='k', label='2 Hz') +ax.plot(t, s1, lw=2, color='r', label='4 Hz') +ax.plot(t, s2, lw=2, color='g', label='6 Hz') plt.subplots_adjust(left=0.2) +lines = ax.get_lines() + +# Make checkbuttons with all plotted lines with correct visibility rax = plt.axes([0.05, 0.4, 0.1, 0.15]) -check = CheckButtons(rax, ('2 Hz', '4 Hz', '6 Hz'), (False, True, True)) +labels = [str(line.get_label()) for line in lines] +visibility = [line.get_visible() for line in lines] +check = CheckButtons(rax, labels, visibility) def func(label): - if label == '2 Hz': - l0.set_visible(not l0.get_visible()) - elif label == '4 Hz': - l1.set_visible(not l1.get_visible()) - elif label == '6 Hz': - l2.set_visible(not l2.get_visible()) + index = labels.index(label) + lines[index].set_visible(not lines[index].get_visible()) plt.draw() + check.on_clicked(func) plt.show()