Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a9ba9d5

Browse files
authored
Merge pull request #24736 from tacaswell/doc/dict_in_checkboxes
DOC: simplify CheckButton example
2 parents 4945f05 + 3a380e5 commit a9ba9d5

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

examples/widgets/check_buttons.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,23 @@
2525
l2, = ax.plot(t, s2, lw=2, color='g', label='6 Hz')
2626
fig.subplots_adjust(left=0.2)
2727

28-
lines = [l0, l1, l2]
28+
lines_by_label = {l.get_label(): l for l in [l0, l1, l2]}
2929

3030
# Make checkbuttons with all plotted lines with correct visibility
3131
rax = fig.add_axes([0.05, 0.4, 0.1, 0.15])
32-
labels = [str(line.get_label()) for line in lines]
33-
visibility = [line.get_visible() for line in lines]
34-
check = CheckButtons(rax, labels, visibility)
32+
check = CheckButtons(
33+
ax=rax,
34+
labels=lines_by_label.keys(),
35+
actives=[l.get_visible() for l in lines_by_label.values()]
36+
)
3537

3638

37-
def func(label):
38-
index = labels.index(label)
39-
lines[index].set_visible(not lines[index].get_visible())
40-
plt.draw()
39+
def callback(label):
40+
ln = lines_by_label[label]
41+
ln.set_visible(not ln.get_visible())
42+
ln.figure.canvas.draw_idle()
4143

42-
check.on_clicked(func)
44+
check.on_clicked(callback)
4345

4446
plt.show()
4547

0 commit comments

Comments
 (0)