Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7aa6979

Browse files
committed
Warn if activecolor and a facecolor are provided
1 parent 0f7bbfe commit 7aa6979

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
@@ -1034,6 +1034,15 @@ def test_radio_buttons_props(fig_test, fig_ref):
10341034
cb.set_radio_props({**radio_props, 's': (24 / 2)**2})
10351035

10361036

1037+
def test_radio_button_active_conflict(ax):
1038+
with pytest.warns(UserWarning,
1039+
match=r'Both the \*activecolor\* parameter'):
1040+
rb = widgets.RadioButtons(ax, ['tea', 'coffee'], activecolor='red',
1041+
radio_props={'facecolor': 'green'})
1042+
# *radio_props*' facecolor wins over *activecolor*
1043+
assert mcolors.same_color(rb._buttons.get_facecolor(), ['green', 'none'])
1044+
1045+
10371046
@check_figures_equal(extensions=["png"])
10381047
def test_check_buttons(fig_test, fig_ref):
10391048
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
@@ -1585,7 +1585,7 @@ class RadioButtons(AxesWidget):
15851585
The label text of the currently selected button.
15861586
"""
15871587

1588-
def __init__(self, ax, labels, active=0, activecolor='blue', *,
1588+
def __init__(self, ax, labels, active=0, activecolor=None, *,
15891589
useblit=True, label_props=None, radio_props=None):
15901590
"""
15911591
Add radio buttons to an `~.axes.Axes`.
@@ -1599,12 +1599,8 @@ def __init__(self, ax, labels, active=0, activecolor='blue', *,
15991599
active : int
16001600
The index of the initially selected button.
16011601
activecolor : color
1602-
The color of the selected button.
1603-
1604-
.. note::
1605-
If a facecolor is supplied in *radio_props*, it will override
1606-
*activecolor*. This may be used to provide an active color per
1607-
button.
1602+
The color of the selected button. The default is ``'blue'`` if not
1603+
specified here or in *radio_props*.
16081604
useblit : bool, default: True
16091605
Use blitting for faster drawing if supported by the backend.
16101606
See the tutorial :doc:`/tutorials/advanced/blitting` for details.
@@ -1621,6 +1617,21 @@ def __init__(self, ax, labels, active=0, activecolor='blue', *,
16211617
button.
16221618
"""
16231619
super().__init__(ax)
1620+
1621+
_api.check_isinstance((dict, None), label_props=label_props,
1622+
radio_props=radio_props)
1623+
1624+
radio_props = cbook.normalize_kwargs(radio_props,
1625+
collections.PathCollection)
1626+
if activecolor is not None:
1627+
if 'facecolor' in radio_props:
1628+
_api.warn_external(
1629+
'Both the *activecolor* parameter and the *facecolor* '
1630+
'key in the *radio_props* parameter has been specified. '
1631+
'*activecolor* will be ignored.')
1632+
else:
1633+
activecolor = 'blue' # Default.
1634+
16241635
self.activecolor = activecolor
16251636
self.value_selected = labels[active]
16261637

@@ -1633,9 +1644,6 @@ def __init__(self, ax, labels, active=0, activecolor='blue', *,
16331644
self._useblit = useblit and self.canvas.supports_blit
16341645
self._background = None
16351646

1636-
_api.check_isinstance((dict, None), label_props=label_props,
1637-
radio_props=radio_props)
1638-
16391647
label_props = _expand_text_props(label_props)
16401648
self.labels = [
16411649
ax.text(0.25, y, label, transform=ax.transAxes,
@@ -1646,7 +1654,7 @@ def __init__(self, ax, labels, active=0, activecolor='blue', *,
16461654

16471655
radio_props = {
16481656
's': text_size**2,
1649-
**cbook.normalize_kwargs(radio_props, collections.PathCollection),
1657+
**radio_props,
16501658
'marker': 'o',
16511659
'transform': ax.transAxes,
16521660
'animated': self._useblit,

0 commit comments

Comments
 (0)