Skip to content

Commit 7247ead

Browse files
committed
Improve button widget examples a bit
Use an inset Axes for the radio buttons, and a mosaic to setup the check buttons. Also, add a note about the styling parameters.
1 parent 2ff0713 commit 7247ead

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

galleries/examples/widgets/check_buttons.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
This program shows the use of `.CheckButtons` which is similar to
99
check boxes. There are 3 different sine waves shown, and we can choose which
1010
waves are displayed with the check buttons.
11+
12+
Check buttons may be styled using the *check_props*, *frame_props*, and *label_props*
13+
parameters. The parameters each take a dictionary with keys of artist property names and
14+
values of lists of settings with length matching the number of buttons.
1115
"""
1216

1317
import matplotlib.pyplot as plt
@@ -24,13 +28,12 @@
2428
l0, = ax.plot(t, s0, visible=False, lw=2, color='black', label='1 Hz')
2529
l1, = ax.plot(t, s1, lw=2, color='red', label='2 Hz')
2630
l2, = ax.plot(t, s2, lw=2, color='green', label='3 Hz')
27-
fig.subplots_adjust(left=0.2)
2831

2932
lines_by_label = {l.get_label(): l for l in [l0, l1, l2]}
3033
line_colors = [l.get_color() for l in lines_by_label.values()]
3134

3235
# Make checkbuttons with all plotted lines with correct visibility
33-
rax = fig.add_axes([0.05, 0.4, 0.1, 0.15])
36+
rax = ax.inset_axes([0.0, 0.0, 0.12, 0.2])
3437
check = CheckButtons(
3538
ax=rax,
3639
labels=lines_by_label.keys(),

galleries/examples/widgets/radio_buttons.py

+23-11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
Radio buttons let you choose between multiple options in a visualization.
99
In this case, the buttons let the user choose one of the three different sine
1010
waves to be shown in the plot.
11+
12+
Radio buttons may be styled using the *label_props* and *radio_props* parameters, which
13+
each take a dictionary with keys of artist property names and values of lists of
14+
settings with length matching the number of buttons.
1115
"""
1216

1317
import matplotlib.pyplot as plt
@@ -20,13 +24,21 @@
2024
s1 = np.sin(4*np.pi*t)
2125
s2 = np.sin(8*np.pi*t)
2226

23-
fig, ax = plt.subplots()
24-
l, = ax.plot(t, s0, lw=2, color='red')
25-
fig.subplots_adjust(left=0.3)
26-
27-
axcolor = 'lightgoldenrodyellow'
28-
rax = fig.add_axes([0.05, 0.7, 0.15, 0.15], facecolor=axcolor)
29-
radio = RadioButtons(rax, ('1 Hz', '2 Hz', '4 Hz'),
27+
fig, ax = plt.subplot_mosaic(
28+
[
29+
['main', 'freq'],
30+
['main', 'color'],
31+
['main', 'linestyle'],
32+
],
33+
width_ratios=[5, 1],
34+
layout='constrained',
35+
)
36+
l, = ax['main'].plot(t, s0, lw=2, color='red')
37+
38+
radio_background = 'lightgoldenrodyellow'
39+
40+
ax['freq'].set_facecolor(radio_background)
41+
radio = RadioButtons(ax['freq'], ('1 Hz', '2 Hz', '4 Hz'),
3042
label_props={'color': 'cmy', 'fontsize': [12, 14, 16]},
3143
radio_props={'s': [16, 32, 64]})
3244

@@ -38,9 +50,9 @@ def hzfunc(label):
3850
fig.canvas.draw()
3951
radio.on_clicked(hzfunc)
4052

41-
rax = fig.add_axes([0.05, 0.4, 0.15, 0.15], facecolor=axcolor)
53+
ax['color'].set_facecolor(radio_background)
4254
radio2 = RadioButtons(
43-
rax, ('red', 'blue', 'green'),
55+
ax['color'], ('red', 'blue', 'green'),
4456
label_props={'color': ['red', 'blue', 'green']},
4557
radio_props={
4658
'facecolor': ['red', 'blue', 'green'],
@@ -53,8 +65,8 @@ def colorfunc(label):
5365
fig.canvas.draw()
5466
radio2.on_clicked(colorfunc)
5567

56-
rax = fig.add_axes([0.05, 0.1, 0.15, 0.15], facecolor=axcolor)
57-
radio3 = RadioButtons(rax, ('-', '--', '-.', ':'))
68+
ax['linestyle'].set_facecolor(radio_background)
69+
radio3 = RadioButtons(ax['linestyle'], ('-', '--', '-.', ':'))
5870

5971

6072
def stylefunc(label):

0 commit comments

Comments
 (0)