@@ -1557,7 +1557,8 @@ class RadioButtons(AxesWidget):
1557
1557
"""
1558
1558
1559
1559
def __init__ (self , ax , labels , active = 0 , activecolor = None , * ,
1560
- useblit = True , label_props = None , radio_props = None ):
1560
+ useblit = True , label_props = None , radio_props = None ,
1561
+ layout_direction = 'vertical' ):
1561
1562
"""
1562
1563
Add radio buttons to an `~.axes.Axes`.
1563
1564
@@ -1593,9 +1594,16 @@ def __init__(self, ax, labels, active=0, activecolor=None, *,
1593
1594
button.
1594
1595
1595
1596
.. versionadded:: 3.7
1597
+ layout_direction : {'vertical', 'horizontal'}
1598
+ The orientation of the buttons: 'vertical' places buttons from top
1599
+ to bottom, 'horizontal' places buttons from left to right.
1600
+
1601
+ .. versionadded:: 3.10
1596
1602
"""
1597
1603
super ().__init__ (ax )
1598
1604
1605
+ _api .check_in_list (['vertical' , 'horizontal' ],
1606
+ layout_direction = layout_direction )
1599
1607
_api .check_isinstance ((dict , None ), label_props = label_props ,
1600
1608
radio_props = radio_props )
1601
1609
@@ -1619,17 +1627,32 @@ def __init__(self, ax, labels, active=0, activecolor=None, *,
1619
1627
ax .set_yticks ([])
1620
1628
ax .set_navigate (False )
1621
1629
1622
- ys = np .linspace (1 , 0 , len (labels ) + 2 )[1 :- 1 ]
1630
+ if layout_direction == 'vertical' :
1631
+ # Place buttons from top to bottom with buttons at (0.15, y) and labels
1632
+ # at (0.25, y), where y is evenly spaced within the Axes.
1633
+ button_ys = label_ys = np .linspace (1 , 0 , len (labels ) + 2 )[1 :- 1 ]
1634
+ button_xs = np .full_like (button_ys , 0.15 )
1635
+ label_xs = np .full_like (label_ys , 0.25 )
1636
+ label_ha = 'left'
1637
+ label_va = 'center'
1638
+ else :
1639
+ # Place buttons from left to right with buttons at (x, 0.15) and labels
1640
+ # at (x, 0.25), where x is evenly spaced within the Axes.
1641
+ button_xs = label_xs = np .linspace (0 , 1 , len (labels ) + 2 )[1 :- 1 ]
1642
+ button_ys = np .full_like (button_xs , 0.15 )
1643
+ label_ys = np .full_like (label_xs , 0.25 )
1644
+ label_ha = 'center'
1645
+ label_va = 'bottom'
1623
1646
1624
1647
self ._useblit = useblit and self .canvas .supports_blit
1625
1648
self ._background = None
1626
1649
1627
1650
label_props = _expand_text_props (label_props )
1628
1651
self .labels = [
1629
- ax .text (0.25 , y , label , transform = ax .transAxes ,
1630
- horizontalalignment = "left" , verticalalignment = "center" ,
1652
+ ax .text (x , y , label , transform = ax .transAxes ,
1653
+ horizontalalignment = label_ha , verticalalignment = label_va ,
1631
1654
** props )
1632
- for y , label , props in zip (ys , labels , label_props )]
1655
+ for x , y , label , props in zip (label_xs , label_ys , labels , label_props )]
1633
1656
text_size = np .array ([text .get_fontsize () for text in self .labels ]) / 2
1634
1657
1635
1658
radio_props = {
@@ -1642,7 +1665,7 @@ def __init__(self, ax, labels, active=0, activecolor=None, *,
1642
1665
radio_props .setdefault ('edgecolor' , radio_props .get ('color' , 'black' ))
1643
1666
radio_props .setdefault ('facecolor' ,
1644
1667
radio_props .pop ('color' , activecolor ))
1645
- self ._buttons = ax .scatter ([ .15 ] * len ( ys ), ys , ** radio_props )
1668
+ self ._buttons = ax .scatter (button_xs , button_ys , ** radio_props )
1646
1669
# The user may have passed custom colours in radio_props, so we need to
1647
1670
# create the radios, and modify the visibility after getting whatever
1648
1671
# the user set.
0 commit comments