@@ -1557,7 +1557,8 @@ class RadioButtons(AxesWidget):
15571557 """
15581558
15591559def __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' ):
15611562"""
15621563 Add radio buttons to an `~.axes.Axes`.
15631564
@@ -1593,9 +1594,16 @@ def __init__(self, ax, labels, active=0, activecolor=None, *,
15931594 button.
15941595
15951596 .. 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
15961602 """
15971603super ().__init__ (ax )
15981604
1605+ _api .check_in_list (['vertical' ,'horizontal' ],
1606+ layout_direction = layout_direction )
15991607_api .check_isinstance ((dict ,None ),label_props = label_props ,
16001608radio_props = radio_props )
16011609
@@ -1619,17 +1627,32 @@ def __init__(self, ax, labels, active=0, activecolor=None, *,
16191627ax .set_yticks ([])
16201628ax .set_navigate (False )
16211629
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'
16231646
16241647self ._useblit = useblit and self .canvas .supports_blit
16251648self ._background = None
16261649
16271650label_props = _expand_text_props (label_props )
16281651self .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 ,
16311654** 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 )]
16331656text_size = np .array ([text .get_fontsize ()for text in self .labels ])/ 2
16341657
16351658radio_props = {
@@ -1642,7 +1665,7 @@ def __init__(self, ax, labels, active=0, activecolor=None, *,
16421665radio_props .setdefault ('edgecolor' ,radio_props .get ('color' ,'black' ))
16431666radio_props .setdefault ('facecolor' ,
16441667radio_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 )
16461669# The user may have passed custom colours in radio_props, so we need to
16471670# create the radios, and modify the visibility after getting whatever
16481671# the user set.