Skip to content

Commit 5f49bd9

Browse files
committed
Add styling support to Check and Radio buttons
1 parent 018c5ef commit 5f49bd9

File tree

4 files changed

+263
-49
lines changed

4 files changed

+263
-49
lines changed

examples/widgets/check_buttons.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
s2 = np.sin(6*np.pi*t)
2121

2222
fig, ax = plt.subplots()
23-
l0, = ax.plot(t, s0, visible=False, lw=2, color='k', label='2 Hz')
24-
l1, = ax.plot(t, s1, lw=2, color='r', label='4 Hz')
25-
l2, = ax.plot(t, s2, lw=2, color='g', label='6 Hz')
23+
l0, = ax.plot(t, s0, visible=False, lw=2, color='k', label='1 Hz')
24+
l1, = ax.plot(t, s1, lw=2, color='r', label='2 Hz')
25+
l2, = ax.plot(t, s2, lw=2, color='g', label='3 Hz')
2626
fig.subplots_adjust(left=0.2)
2727

2828
lines_by_label = {l.get_label(): l for l in [l0, l1, l2]}
@@ -32,7 +32,10 @@
3232
check = CheckButtons(
3333
ax=rax,
3434
labels=lines_by_label.keys(),
35-
actives=[l.get_visible() for l in lines_by_label.values()]
35+
actives=[l.get_visible() for l in lines_by_label.values()],
36+
label_props={'color': ['k', 'r', 'g']},
37+
frame_props={'edgecolor': ['k', 'r', 'g']},
38+
check_props={'facecolor': ['k', 'r', 'g']},
3639
)
3740

3841

examples/widgets/radio_buttons.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,31 @@
2525

2626
axcolor = 'lightgoldenrodyellow'
2727
rax = fig.add_axes([0.05, 0.7, 0.15, 0.15], facecolor=axcolor)
28-
radio = RadioButtons(rax, ('2 Hz', '4 Hz', '8 Hz'))
28+
radio = RadioButtons(rax, ('1 Hz', '2 Hz', '4 Hz'),
29+
label_props={'color': 'cmy', 'fontsize': [12, 18, 24]},
30+
radio_props={'s': [16, 32, 64]})
2931

3032

3133
def hzfunc(label):
32-
hzdict = {'2 Hz': s0, '4 Hz': s1, '8 Hz': s2}
34+
hzdict = {'1 Hz': s0, '2 Hz': s1, '4 Hz': s2}
3335
ydata = hzdict[label]
3436
l.set_ydata(ydata)
35-
plt.draw()
37+
fig.canvas.draw()
3638
radio.on_clicked(hzfunc)
3739

3840
rax = fig.add_axes([0.05, 0.4, 0.15, 0.15], facecolor=axcolor)
39-
radio2 = RadioButtons(rax, ('red', 'blue', 'green'))
41+
radio2 = RadioButtons(
42+
rax, ('red', 'blue', 'green'),
43+
label_props={'color': ['red', 'blue', 'green']},
44+
radio_props={
45+
'facecolor': ['red', 'blue', 'green'],
46+
'edgecolor': ['darkred', 'darkblue', 'darkgreen'],
47+
})
4048

4149

4250
def colorfunc(label):
4351
l.set_color(label)
44-
plt.draw()
52+
fig.canvas.draw()
4553
radio2.on_clicked(colorfunc)
4654

4755
rax = fig.add_axes([0.05, 0.1, 0.15, 0.15], facecolor=axcolor)
@@ -50,7 +58,7 @@ def colorfunc(label):
5058

5159
def stylefunc(label):
5260
l.set_linestyle(label)
53-
plt.draw()
61+
fig.canvas.draw()
5462
radio3.on_clicked(stylefunc)
5563

5664
plt.show()

lib/matplotlib/tests/test_widgets.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,21 @@ def test_radio_buttons(fig_test, fig_ref):
10171017
ax.text(.25, 1/3, "coffee", transform=ax.transAxes, va="center")
10181018

10191019

1020+
@check_figures_equal(extensions=['png'])
1021+
def test_radio_buttons_props(fig_test, fig_ref):
1022+
label_props = {'color': ['red'], 'fontsize': [24]}
1023+
radio_props = {'facecolor': 'green', 'edgecolor': 'blue', 'linewidth': 2}
1024+
1025+
widgets.RadioButtons(fig_ref.subplots(), ['tea', 'coffee'],
1026+
label_props=label_props, radio_props=radio_props)
1027+
1028+
cb = widgets.RadioButtons(fig_test.subplots(), ['tea', 'coffee'])
1029+
cb.set_label_props(label_props)
1030+
# Setting the label size automatically increases default marker size, so we
1031+
# need to do that here as well.
1032+
cb.set_radio_props({**radio_props, 's': (24 / 2)**2})
1033+
1034+
10201035
@check_figures_equal(extensions=["png"])
10211036
def test_check_buttons(fig_test, fig_ref):
10221037
widgets.CheckButtons(fig_test.subplots(), ["tea", "coffee"], [True, True])
@@ -1029,6 +1044,29 @@ def test_check_buttons(fig_test, fig_ref):
10291044
ax.text(.25, 1/3, "coffee", transform=ax.transAxes, va="center")
10301045

10311046

1047+
@check_figures_equal(extensions=['png'])
1048+
def test_check_button_props(fig_test, fig_ref):
1049+
label_props = {'color': ['red'], 'fontsize': [24]}
1050+
frame_props = {'facecolor': 'green', 'edgecolor': 'blue', 'linewidth': 2}
1051+
check_props = {'facecolor': 'red', 'linewidth': 2}
1052+
1053+
widgets.CheckButtons(fig_ref.subplots(), ['tea', 'coffee'], [True, True],
1054+
label_props=label_props, frame_props=frame_props,
1055+
check_props=check_props)
1056+
1057+
cb = widgets.CheckButtons(fig_test.subplots(), ['tea', 'coffee'],
1058+
[True, True])
1059+
cb.set_label_props(label_props)
1060+
# Setting the label size automatically increases default marker size, so we
1061+
# need to do that here as well.
1062+
cb.set_frame_props({**frame_props, 's': (24 / 2)**2})
1063+
# FIXME: Axes.scatter promotes facecolor to edgecolor on unfilled markers,
1064+
# but Collection.update doesn't do that (it forgot the marker already).
1065+
# This means we cannot pass facecolor to both setters directly.
1066+
check_props['edgecolor'] = check_props.pop('facecolor')
1067+
cb.set_check_props({**check_props, 's': (24 / 2)**2})
1068+
1069+
10321070
@check_figures_equal(extensions=["png"])
10331071
def test_check_buttons_rectangles(fig_test, fig_ref):
10341072
# Test should be removed once .rectangles is removed

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy