@@ -150,6 +150,8 @@ def __init__(self, parent, handles, labels,
150
150
numpoints = None , # the number of points in the legend line
151
151
markerscale = None , # the relative size of legend markers
152
152
# vs. original
153
+ markerfirst = True , # controls ordering (left-to-right) of
154
+ # legend marker and label
153
155
scatterpoints = None , # number of scatter points
154
156
scatteryoffsets = None ,
155
157
prop = None , # properties for the legend texts
@@ -198,6 +200,8 @@ def __init__(self, parent, handles, labels,
198
200
prop the font property
199
201
fontsize the font size (used only if prop is not specified)
200
202
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
201
205
numpoints the number of points in the legend for line
202
206
scatterpoints the number of points in the legend for scatter plot
203
207
scatteryoffsets a list of yoffsets for scatter symbols in legend
@@ -365,7 +369,7 @@ def __init__(self, parent, handles, labels,
365
369
self ._drawFrame = rcParams ["legend.frameon" ]
366
370
367
371
# init with null renderer
368
- self ._init_legend_box (handles , labels )
372
+ self ._init_legend_box (handles , labels , markerfirst )
369
373
370
374
if framealpha is None :
371
375
self .get_frame ().set_alpha (rcParams ["legend.framealpha" ])
@@ -558,7 +562,7 @@ def get_legend_handler(legend_handler_map, orig_handle):
558
562
559
563
return handler
560
564
561
- def _init_legend_box (self , handles , labels ):
565
+ def _init_legend_box (self , handles , labels , markerfirst = True ):
562
566
"""
563
567
Initialize the legend_box. The legend_box is an instance of
564
568
the OffsetBox, which is packed with legend handles and
@@ -669,16 +673,24 @@ def _init_legend_box(self, handles, labels):
669
673
# pack handleBox and labelBox into itemBox
670
674
itemBoxes = [HPacker (pad = 0 ,
671
675
sep = self .handletextpad * fontsize ,
672
- children = [h , t ], align = "baseline" )
676
+ children = [h , t ] if markerfirst else [t , h ],
677
+ align = "baseline" )
673
678
for h , t in handle_label [i0 :i0 + di ]]
674
679
# 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 )
676
684
677
685
# pack columnBox
686
+ if markerfirst :
687
+ alignment = "baseline"
688
+ else :
689
+ alignment = "right"
678
690
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 ))
682
694
683
695
if self ._mode == "expand" :
684
696
mode = "expand"
0 commit comments