Skip to content

Commit 1a77aa6

Browse files
committed
Warn if activecolor and a facecolor are provided
1 parent 1a31f4f commit 1a77aa6

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

lib/matplotlib/tests/test_widgets.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,15 @@ def test_radio_buttons_props(fig_test, fig_ref):
10511051
cb.set_radio_props({**radio_props, 's': (24 / 2)**2})
10521052

10531053

1054+
def test_radio_button_active_conflict(ax):
1055+
with pytest.warns(UserWarning,
1056+
match=r'Both the \*activecolor\* parameter'):
1057+
rb = widgets.RadioButtons(ax, ['tea', 'coffee'], activecolor='red',
1058+
radio_props={'facecolor': 'green'})
1059+
# *radio_props*' facecolor wins over *activecolor*
1060+
assert mcolors.same_color(rb._buttons.get_facecolor(), ['green', 'none'])
1061+
1062+
10541063
@check_figures_equal(extensions=["png"])
10551064
def test_check_buttons(fig_test, fig_ref):
10561065
widgets.CheckButtons(fig_test.subplots(), ["tea", "coffee"], [True, True])

lib/matplotlib/widgets.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,7 +1587,7 @@ class RadioButtons(AxesWidget):
15871587
The label text of the currently selected button.
15881588
"""
15891589

1590-
def __init__(self, ax, labels, active=0, activecolor='blue', *,
1590+
def __init__(self, ax, labels, active=0, activecolor=None, *,
15911591
useblit=True, label_props=None, radio_props=None):
15921592
"""
15931593
Add radio buttons to an `~.axes.Axes`.
@@ -1601,12 +1601,8 @@ def __init__(self, ax, labels, active=0, activecolor='blue', *,
16011601
active : int
16021602
The index of the initially selected button.
16031603
activecolor : color
1604-
The color of the selected button.
1605-
1606-
.. note::
1607-
If a facecolor is supplied in *radio_props*, it will override
1608-
*activecolor*. This may be used to provide an active color per
1609-
button.
1604+
The color of the selected button. The default is ``'blue'`` if not
1605+
specified here or in *radio_props*.
16101606
useblit : bool, default: True
16111607
Use blitting for faster drawing if supported by the backend.
16121608
See the tutorial :doc:`/tutorials/advanced/blitting` for details.
@@ -1623,6 +1619,21 @@ def __init__(self, ax, labels, active=0, activecolor='blue', *,
16231619
button.
16241620
"""
16251621
super().__init__(ax)
1622+
1623+
_api.check_isinstance((dict, None), label_props=label_props,
1624+
radio_props=radio_props)
1625+
1626+
radio_props = cbook.normalize_kwargs(radio_props,
1627+
collections.PathCollection)
1628+
if activecolor is not None:
1629+
if 'facecolor' in radio_props:
1630+
_api.warn_external(
1631+
'Both the *activecolor* parameter and the *facecolor* '
1632+
'key in the *radio_props* parameter has been specified. '
1633+
'*activecolor* will be ignored.')
1634+
else:
1635+
activecolor = 'blue' # Default.
1636+
16261637
self.activecolor = activecolor
16271638
self.value_selected = labels[active]
16281639

@@ -1635,9 +1646,6 @@ def __init__(self, ax, labels, active=0, activecolor='blue', *,
16351646
self._useblit = useblit and self.canvas.supports_blit
16361647
self._background = None
16371648

1638-
_api.check_isinstance((dict, None), label_props=label_props,
1639-
radio_props=radio_props)
1640-
16411649
label_props = _expand_text_props(label_props)
16421650
self.labels = [
16431651
ax.text(0.25, y, label, transform=ax.transAxes,
@@ -1648,7 +1656,7 @@ def __init__(self, ax, labels, active=0, activecolor='blue', *,
16481656

16491657
radio_props = {
16501658
's': text_size**2,
1651-
**cbook.normalize_kwargs(radio_props, collections.PathCollection),
1659+
**radio_props,
16521660
'marker': 'o',
16531661
'transform': ax.transAxes,
16541662
'animated': self._useblit,

0 commit comments

Comments
 (0)