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

Skip to content

Commit 6319ba9

Browse files
committed
Warn if activecolor and a facecolor are provided
1 parent 5ae09a2 commit 6319ba9

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
@@ -1596,7 +1596,7 @@ class RadioButtons(AxesWidget):
15961596
The label text of the currently selected button.
15971597
"""
15981598

1599-
def __init__(self, ax, labels, active=0, activecolor='blue', *,
1599+
def __init__(self, ax, labels, active=0, activecolor=None, *,
16001600
useblit=True, label_props=None, radio_props=None):
16011601
"""
16021602
Add radio buttons to an `~.axes.Axes`.
@@ -1610,12 +1610,8 @@ def __init__(self, ax, labels, active=0, activecolor='blue', *,
16101610
active : int
16111611
The index of the initially selected button.
16121612
activecolor : color
1613-
The color of the selected button.
1614-
1615-
.. note::
1616-
If a facecolor is supplied in *radio_props*, it will override
1617-
*activecolor*. This may be used to provide an active color per
1618-
button.
1613+
The color of the selected button. The default is ``'blue'`` if not
1614+
specified here or in *radio_props*.
16191615
useblit : bool, default: True
16201616
Use blitting for faster drawing if supported by the backend.
16211617
See the tutorial :doc:`/tutorials/advanced/blitting` for details.
@@ -1632,6 +1628,21 @@ def __init__(self, ax, labels, active=0, activecolor='blue', *,
16321628
button.
16331629
"""
16341630
super().__init__(ax)
1631+
1632+
_api.check_isinstance((dict, None), label_props=label_props,
1633+
radio_props=radio_props)
1634+
1635+
radio_props = cbook.normalize_kwargs(radio_props,
1636+
collections.PathCollection)
1637+
if activecolor is not None:
1638+
if 'facecolor' in radio_props:
1639+
_api.warn_external(
1640+
'Both the *activecolor* parameter and the *facecolor* '
1641+
'key in the *radio_props* parameter has been specified. '
1642+
'*activecolor* will be ignored.')
1643+
else:
1644+
activecolor = 'blue' # Default.
1645+
16351646
self.activecolor = activecolor
16361647
self.value_selected = labels[active]
16371648

@@ -1644,9 +1655,6 @@ def __init__(self, ax, labels, active=0, activecolor='blue', *,
16441655
self._useblit = useblit and self.canvas.supports_blit
16451656
self._background = None
16461657

1647-
_api.check_isinstance((dict, None), label_props=label_props,
1648-
radio_props=radio_props)
1649-
16501658
label_props = _expand_text_props(label_props)
16511659
self.labels = [
16521660
ax.text(0.25, y, label, transform=ax.transAxes,
@@ -1660,7 +1668,7 @@ def __init__(self, ax, labels, active=0, activecolor='blue', *,
16601668
's': text_size**2,
16611669
'facecolor': activecolor,
16621670
'edgecolor': 'black',
1663-
**cbook.normalize_kwargs(radio_props, collections.PathCollection),
1671+
**radio_props,
16641672
'transform': ax.transAxes,
16651673
'animated': self._useblit,
16661674
}

0 commit comments

Comments
 (0)