@@ -1580,7 +1580,8 @@ class RadioButtons(AxesWidget):
1580
1580
"""
1581
1581
1582
1582
def __init__ (self , ax , labels , active = 0 , activecolor = None , * ,
1583
- useblit = True , label_props = None , radio_props = None ):
1583
+ useblit = True , label_props = None , radio_props = None ,
1584
+ orientation = 'vertical' ):
1584
1585
"""
1585
1586
Add radio buttons to an `~.axes.Axes`.
1586
1587
@@ -1616,9 +1617,15 @@ def __init__(self, ax, labels, active=0, activecolor=None, *,
1616
1617
button.
1617
1618
1618
1619
.. versionadded:: 3.7
1620
+ orientation : {'vertical', 'horizontal'}
1621
+ The orientation of the buttons: 'vertical' places buttons from top
1622
+ to bottom, 'horizontal' places buttons from left to right.
1623
+
1624
+ .. versionadded:: 3.9
1619
1625
"""
1620
1626
super ().__init__ (ax )
1621
1627
1628
+ _api .check_in_list (['vertical' , 'horizontal' ], orientation = orientation )
1622
1629
_api .check_isinstance ((dict , None ), label_props = label_props ,
1623
1630
radio_props = radio_props )
1624
1631
@@ -1642,17 +1649,32 @@ def __init__(self, ax, labels, active=0, activecolor=None, *,
1642
1649
ax .set_yticks ([])
1643
1650
ax .set_navigate (False )
1644
1651
1645
- ys = np .linspace (1 , 0 , len (labels ) + 2 )[1 :- 1 ]
1652
+ if orientation == 'vertical' :
1653
+ # Place buttons from top to bottom with buttons at (0.15, y) and labels
1654
+ # at (0.25, y), where y is evenly spaced within the Axes.
1655
+ button_ys = label_ys = np .linspace (1 , 0 , len (labels ) + 2 )[1 :- 1 ]
1656
+ button_xs = np .full_like (button_ys , 0.15 )
1657
+ label_xs = np .full_like (label_ys , 0.25 )
1658
+ label_ha = 'left'
1659
+ label_va = 'center'
1660
+ else :
1661
+ # Place buttons from left to right with buttons at (x, 0.15) and labels
1662
+ # at (x, 0.25), where x is evenly spaced within the Axes.
1663
+ button_xs = label_xs = np .linspace (0 , 1 , len (labels ) + 2 )[1 :- 1 ]
1664
+ button_ys = np .full_like (button_xs , 0.15 )
1665
+ label_ys = np .full_like (label_xs , 0.25 )
1666
+ label_ha = 'center'
1667
+ label_va = 'bottom'
1646
1668
1647
1669
self ._useblit = useblit and self .canvas .supports_blit
1648
1670
self ._background = None
1649
1671
1650
1672
label_props = _expand_text_props (label_props )
1651
1673
self .labels = [
1652
- ax .text (0.25 , y , label , transform = ax .transAxes ,
1653
- horizontalalignment = "left" , verticalalignment = "center" ,
1674
+ ax .text (x , y , label , transform = ax .transAxes ,
1675
+ horizontalalignment = label_ha , verticalalignment = label_va ,
1654
1676
** props )
1655
- for y , label , props in zip (ys , labels , label_props )]
1677
+ for x , y , label , props in zip (label_xs , label_ys , labels , label_props )]
1656
1678
text_size = np .array ([text .get_fontsize () for text in self .labels ]) / 2
1657
1679
1658
1680
radio_props = {
@@ -1665,7 +1687,7 @@ def __init__(self, ax, labels, active=0, activecolor=None, *,
1665
1687
radio_props .setdefault ('edgecolor' , radio_props .get ('color' , 'black' ))
1666
1688
radio_props .setdefault ('facecolor' ,
1667
1689
radio_props .pop ('color' , activecolor ))
1668
- self ._buttons = ax .scatter ([ .15 ] * len ( ys ), ys , ** radio_props )
1690
+ self ._buttons = ax .scatter (button_xs , button_ys , ** radio_props )
1669
1691
# The user may have passed custom colours in radio_props, so we need to
1670
1692
# create the radios, and modify the visibility after getting whatever
1671
1693
# the user set.
0 commit comments