@@ -150,6 +150,8 @@ def __init__(self, parent, handles, labels,
150150 numpoints = None , # the number of points in the legend line
151151 markerscale = None , # the relative size of legend markers
152152 # vs. original
153+ markerfirst = True , # controls ordering (left-to-right) of
154+ # legend marker and label
153155 scatterpoints = None , # number of scatter points
154156 scatteryoffsets = None ,
155157 prop = None , # properties for the legend texts
@@ -198,6 +200,8 @@ def __init__(self, parent, handles, labels,
198200 prop the font property
199201 fontsize the font size (used only if prop is not specified)
200202 markerscale the relative size of legend markers vs. original
203+ markerfirst If true, place legend marker to left of label
204+ If false, place legend marker to right of label
201205 numpoints the number of points in the legend for line
202206 scatterpoints the number of points in the legend for scatter plot
203207 scatteryoffsets a list of yoffsets for scatter symbols in legend
@@ -365,7 +369,7 @@ def __init__(self, parent, handles, labels,
365369 self ._drawFrame = rcParams ["legend.frameon" ]
366370
367371 # init with null renderer
368- self ._init_legend_box (handles , labels )
372+ self ._init_legend_box (handles , labels , markerfirst )
369373
370374 if framealpha is None :
371375 self .get_frame ().set_alpha (rcParams ["legend.framealpha" ])
@@ -558,7 +562,7 @@ def get_legend_handler(legend_handler_map, orig_handle):
558562
559563 return handler
560564
561- def _init_legend_box (self , handles , labels ):
565+ def _init_legend_box (self , handles , labels , markerfirst = True ):
562566 """
563567 Initialize the legend_box. The legend_box is an instance of
564568 the OffsetBox, which is packed with legend handles and
@@ -669,16 +673,24 @@ def _init_legend_box(self, handles, labels):
669673 # pack handleBox and labelBox into itemBox
670674 itemBoxes = [HPacker (pad = 0 ,
671675 sep = self .handletextpad * fontsize ,
672- children = [h , t ], align = "baseline" )
676+ children = [h , t ] if markerfirst else [t , h ],
677+ align = "baseline" )
673678 for h , t in handle_label [i0 :i0 + di ]]
674679 # minimumdescent=False for the text of the last row of the column
675- itemBoxes [- 1 ].get_children ()[1 ].set_minimumdescent (False )
680+ if markerfirst :
681+ itemBoxes [- 1 ].get_children ()[1 ].set_minimumdescent (False )
682+ else :
683+ itemBoxes [- 1 ].get_children ()[0 ].set_minimumdescent (False )
676684
677685 # pack columnBox
686+ if markerfirst :
687+ alignment = "baseline"
688+ else :
689+ alignment = "right"
678690 columnbox .append (VPacker (pad = 0 ,
679- sep = self .labelspacing * fontsize ,
680- align = "baseline" ,
681- children = itemBoxes ))
691+ sep = self .labelspacing * fontsize ,
692+ align = alignment ,
693+ children = itemBoxes ))
682694
683695 if self ._mode == "expand" :
684696 mode = "expand"
0 commit comments