Skip to content

Commit f772d0a

Browse files
committed
RadioButtons: Fix label_props argument and set_label_props method
Fixes #30393
1 parent 2b0f3dc commit f772d0a

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Improve Label Properties Settings in ``RadioButtons``
2+
-----------------------------------------------------
3+
4+
Since styling was introduced to `RadioButtons` in version 3.7.0, the
5+
``label_props`` argument of `~.RadioButtons.__init__` and the
6+
`~.RadioButtons.set_label_props` class method, both required a single element
7+
list per every property in the given dictionary. Now these dictionary values
8+
can be a scalar, which will be used to all radio button labels, or a list. In
9+
case of a list, every value in the list will be used per label, and the list's
10+
length should fit the amount of labels. A list with an improper amount of items
11+
raises an error message.

lib/matplotlib/tests/test_widgets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1177,7 +1177,7 @@ def test_check_buttons(fig_test, fig_ref):
11771177

11781178
@check_figures_equal()
11791179
def test_check_button_props(fig_test, fig_ref):
1180-
label_props = {'color': ['red'], 'fontsize': [24]}
1180+
label_props = {'color': 'red', 'fontsize': 24}
11811181
frame_props = {'facecolor': 'green', 'edgecolor': 'blue', 'linewidth': 2}
11821182
check_props = {'facecolor': 'red', 'linewidth': 2}
11831183

lib/matplotlib/widgets.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,11 @@ def __init__(self, ax, labels, actives=None, *, useblit=True,
10171017
.. versionadded:: 3.7
10181018
10191019
label_props : dict, optional
1020-
Dictionary of `.Text` properties to be used for the labels.
1020+
Dictionary of `.Text` properties to be used for the labels. Per dict
1021+
key, if the value is a list or a tuple, its length should be the
1022+
amount of labels, and then a value per label is set. If the dict's
1023+
value is not a list or a tuple, the same property value is applied
1024+
to all labels
10211025
10221026
.. versionadded:: 3.7
10231027
frame_props : dict, optional
@@ -1050,12 +1054,12 @@ def __init__(self, ax, labels, actives=None, *, useblit=True,
10501054

10511055
ys = np.linspace(1, 0, len(labels)+2)[1:-1]
10521056

1053-
label_props = _expand_text_props(label_props)
10541057
self.labels = [
10551058
ax.text(0.25, y, label, transform=ax.transAxes,
10561059
horizontalalignment="left", verticalalignment="center",
1057-
**props)
1058-
for y, label, props in zip(ys, labels, label_props)]
1060+
)
1061+
for y, label in zip(ys, labels)]
1062+
self.set_label_props(label_props)
10591063
text_size = np.array([text.get_fontsize() for text in self.labels]) / 2
10601064

10611065
frame_props = {
@@ -1117,12 +1121,26 @@ def set_label_props(self, props):
11171121
Parameters
11181122
----------
11191123
props : dict
1120-
Dictionary of `.Text` properties to be used for the labels.
1124+
Dictionary of `.Text` properties, just like `label_props` argument
1125+
of __init__ function.
11211126
"""
11221127
_api.check_isinstance(dict, props=props)
11231128
props = _expand_text_props(props)
1124-
for text, prop in zip(self.labels, props):
1125-
text.update(prop)
1129+
for propk,propv in props.items():
1130+
if isinstance(propv, (list, tuple)):
1131+
if len(propv) != len(self.labels):
1132+
raise ValueError(
1133+
f"When setting labels' property {propk}, we "
1134+
f"encountered a {type(propv)} as a label dict value, "
1135+
"with a length different from the amount of labels. "
1136+
"Please use a single value, or a list of values per "
1137+
"label."
1138+
)
1139+
for text,propv_single in zip(self.labels, propv):
1140+
text.update({propk: propv_single})
1141+
else:
1142+
for text in self.labels:
1143+
text.update({propk: propv})
11261144

11271145
def set_frame_props(self, props):
11281146
"""

0 commit comments

Comments
 (0)