From ef29e676b4f24f9a9213aac7996ea5ae3cef0180 Mon Sep 17 00:00:00 2001 From: BTWS Date: Sun, 17 Apr 2016 10:10:55 +0200 Subject: [PATCH 1/3] Make checkbuttons with all plotted lines with correct visibility automatically Previously you had to manually change the variables in func. Now it's done automatically based on the properties of the lines. --- examples/widgets/check_buttons.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/examples/widgets/check_buttons.py b/examples/widgets/check_buttons.py index 936e6d96341c..f26fe7dd78d2 100644 --- a/examples/widgets/check_buttons.py +++ b/examples/widgets/check_buttons.py @@ -8,23 +8,23 @@ 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) -rax = plt.axes([0.05, 0.4, 0.1, 0.15]) -check = CheckButtons(rax, ('2 Hz', '4 Hz', '6 Hz'), (False, True, True)) +lines = ax.get_lines() +# Make checkbuttons with all plotted lines with correct visibility +rax = plt.axes([0.05, 0.4, 0.1, 0.15]) +labels = [str(graph.get_label()) for graph in lines] +visibility = [graph.get_visible() for graph 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()) + lines[labels.index(label)].set_visible(not lines[labels.index(label)].get_visible()) plt.draw() + check.on_clicked(func) plt.show() From 13ff1f7de7d0fa452f7cf9f927cb50ce9c378304 Mon Sep 17 00:00:00 2001 From: BTWS Date: Sun, 17 Apr 2016 10:18:32 +0200 Subject: [PATCH 2/3] Update check_buttons.py --- examples/widgets/check_buttons.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/widgets/check_buttons.py b/examples/widgets/check_buttons.py index f26fe7dd78d2..1a2d4c32d642 100644 --- a/examples/widgets/check_buttons.py +++ b/examples/widgets/check_buttons.py @@ -17,8 +17,8 @@ # Make checkbuttons with all plotted lines with correct visibility rax = plt.axes([0.05, 0.4, 0.1, 0.15]) -labels = [str(graph.get_label()) for graph in lines] -visibility = [graph.get_visible() for graph in lines] +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): From 0ee87e686da4d165b7070b4463ca092c12ff9666 Mon Sep 17 00:00:00 2001 From: BTWS Date: Sun, 17 Apr 2016 10:56:07 +0200 Subject: [PATCH 3/3] Fix PEP8 E302, E501 and W292 --- examples/widgets/check_buttons.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/widgets/check_buttons.py b/examples/widgets/check_buttons.py index 1a2d4c32d642..c259490e4a6d 100644 --- a/examples/widgets/check_buttons.py +++ b/examples/widgets/check_buttons.py @@ -21,8 +21,10 @@ visibility = [line.get_visible() for line in lines] check = CheckButtons(rax, labels, visibility) + def func(label): - lines[labels.index(label)].set_visible(not lines[labels.index(label)].get_visible()) + index = labels.index(label) + lines[index].set_visible(not lines[index].get_visible()) plt.draw() check.on_clicked(func)