38
38
from matplotlib .font_manager import FontProperties
39
39
from matplotlib .lines import Line2D
40
40
from matplotlib .patches import Patch , Rectangle , Shadow , FancyBboxPatch
41
- from matplotlib .collections import LineCollection , RegularPolyCollection , \
42
- CircleCollection , PathCollection , PolyCollection
41
+ from matplotlib .collections import (LineCollection , RegularPolyCollection ,
42
+ CircleCollection , PathCollection ,
43
+ PolyCollection )
43
44
from matplotlib .transforms import Bbox , BboxBase , TransformedBbox
44
45
from matplotlib .transforms import BboxTransformTo , BboxTransformFrom
45
46
@@ -93,7 +94,8 @@ def _update_loc(self, loc_in_canvas):
93
94
94
95
_bbox_transform = BboxTransformFrom (bbox )
95
96
self .legend ._loc = tuple (
96
- _bbox_transform .transform_point (loc_in_canvas ))
97
+ _bbox_transform .transform_point (loc_in_canvas )
98
+ )
97
99
98
100
def _update_bbox_to_anchor (self , loc_in_canvas ):
99
101
@@ -150,6 +152,8 @@ def __init__(self, parent, handles, labels,
150
152
numpoints = None , # the number of points in the legend line
151
153
markerscale = None , # the relative size of legend markers
152
154
# vs. original
155
+ markerfirst = True , # controls ordering (left-to-right) of
156
+ # legend marker and label
153
157
scatterpoints = None , # number of scatter points
154
158
scatteryoffsets = None ,
155
159
prop = None , # properties for the legend texts
@@ -176,7 +180,7 @@ def __init__(self, parent, handles, labels,
176
180
shadow = None ,
177
181
title = None , # set a title for the legend
178
182
179
- framealpha = None , # set frame alpha
183
+ framealpha = None , # set frame alpha
180
184
181
185
bbox_to_anchor = None , # bbox that the legend will be anchored.
182
186
bbox_transform = None , # transform for the bbox
@@ -198,6 +202,8 @@ def __init__(self, parent, handles, labels,
198
202
prop the font property
199
203
fontsize the font size (used only if prop is not specified)
200
204
markerscale the relative size of legend markers vs. original
205
+ markerfirst If true, place legend marker to left of label
206
+ If false, place legend marker to right of label
201
207
numpoints the number of points in the legend for line
202
208
scatterpoints the number of points in the legend for scatter plot
203
209
scatteryoffsets a list of yoffsets for scatter symbols in legend
@@ -316,13 +322,15 @@ def __init__(self, parent, handles, labels,
316
322
if self .isaxes :
317
323
warnings .warn ('Unrecognized location "%s". Falling back '
318
324
'on "best"; valid locations are\n \t %s\n '
319
- % (loc , '\n \t ' .join (six .iterkeys (self .codes ))))
325
+ % (loc , '\n \t ' .join (
326
+ six .iterkeys (self .codes ))))
320
327
loc = 0
321
328
else :
322
329
warnings .warn ('Unrecognized location "%s". Falling back '
323
330
'on "upper right"; '
324
331
'valid locations are\n \t %s\n '
325
- % (loc , '\n \t ' .join (six .iterkeys (self .codes ))))
332
+ % (loc , '\n \t ' .join (
333
+ six .iterkeys (self .codes ))))
326
334
loc = 1
327
335
else :
328
336
loc = self .codes [loc ]
@@ -365,7 +373,7 @@ def __init__(self, parent, handles, labels,
365
373
self ._drawFrame = rcParams ["legend.frameon" ]
366
374
367
375
# init with null renderer
368
- self ._init_legend_box (handles , labels )
376
+ self ._init_legend_box (handles , labels , markerfirst )
369
377
370
378
if framealpha is None :
371
379
self .get_frame ().set_alpha (rcParams ["legend.framealpha" ])
@@ -396,8 +404,8 @@ def _set_loc(self, loc):
396
404
else :
397
405
_findoffset = self ._findoffset_loc
398
406
399
- # def findoffset(width, height, xdescent, ydescent):
400
- # return _findoffset(width, height, xdescent, ydescent, renderer)
407
+ # def findoffset(width, height, xdescent, ydescent):
408
+ # return _findoffset(width, height, xdescent, ydescent, renderer)
401
409
402
410
self ._legend_box .set_offset (_findoffset )
403
411
@@ -485,7 +493,7 @@ def _approx_text_height(self, renderer=None):
485
493
RegularPolyCollection : legend_handler .HandlerRegularPolyCollection (),
486
494
CircleCollection : legend_handler .HandlerCircleCollection (),
487
495
BarContainer : legend_handler .HandlerPatch (
488
- update_func = legend_handler .update_from_first_child ),
496
+ update_func = legend_handler .update_from_first_child ),
489
497
tuple : legend_handler .HandlerTuple (),
490
498
PathCollection : legend_handler .HandlerPathCollection (),
491
499
PolyCollection : legend_handler .HandlerPolyCollection ()
@@ -558,7 +566,7 @@ def get_legend_handler(legend_handler_map, orig_handle):
558
566
559
567
return handler
560
568
561
- def _init_legend_box (self , handles , labels ):
569
+ def _init_legend_box (self , handles , labels , markerfirst = True ):
562
570
"""
563
571
Initialize the legend_box. The legend_box is an instance of
564
572
the OffsetBox, which is packed with legend handles and
@@ -606,10 +614,11 @@ def _init_legend_box(self, handles, labels):
606
614
handler = self .get_legend_handler (legend_handler_map , orig_handle )
607
615
if handler is None :
608
616
warnings .warn (
609
- "Legend does not support {!r} instances.\n A proxy artist "
610
- "may be used instead.\n See: "
611
- "http://matplotlib.org/users/legend_guide.html"
612
- "#using-proxy-artist" .format (orig_handle ))
617
+ "Legend does not support {!r} instances.\n A proxy artist "
618
+ "may be used instead.\n See: "
619
+ "http://matplotlib.org/users/legend_guide.html"
620
+ "#using-proxy-artist" .format (orig_handle )
621
+ )
613
622
# We don't have a handle for this artist, so we just defer
614
623
# to None.
615
624
handle_list .append (None )
@@ -654,11 +663,10 @@ def _init_legend_box(self, handles, labels):
654
663
# starting index of each column and number of rows in it.
655
664
largecol = safezip (list (xrange (0 ,
656
665
num_largecol * (nrows + 1 ),
657
- (nrows + 1 ))),
666
+ (nrows + 1 ))),
658
667
[nrows + 1 ] * num_largecol )
659
668
smallcol = safezip (list (xrange (num_largecol * (nrows + 1 ),
660
- len (handleboxes ),
661
- nrows )),
669
+ len (handleboxes ), nrows )),
662
670
[nrows ] * num_smallcol )
663
671
else :
664
672
largecol , smallcol = [], []
@@ -669,16 +677,24 @@ def _init_legend_box(self, handles, labels):
669
677
# pack handleBox and labelBox into itemBox
670
678
itemBoxes = [HPacker (pad = 0 ,
671
679
sep = self .handletextpad * fontsize ,
672
- children = [h , t ], align = "baseline" )
680
+ children = [h , t ] if markerfirst else [t , h ],
681
+ align = "baseline" )
673
682
for h , t in handle_label [i0 :i0 + di ]]
674
683
# minimumdescent=False for the text of the last row of the column
675
- itemBoxes [- 1 ].get_children ()[1 ].set_minimumdescent (False )
684
+ if markerfirst :
685
+ itemBoxes [- 1 ].get_children ()[1 ].set_minimumdescent (False )
686
+ else :
687
+ itemBoxes [- 1 ].get_children ()[0 ].set_minimumdescent (False )
676
688
677
689
# pack columnBox
690
+ if markerfirst :
691
+ alignment = "baseline"
692
+ else :
693
+ alignment = "right"
678
694
columnbox .append (VPacker (pad = 0 ,
679
- sep = self .labelspacing * fontsize ,
680
- align = "baseline" ,
681
- children = itemBoxes ))
695
+ sep = self .labelspacing * fontsize ,
696
+ align = alignment ,
697
+ children = itemBoxes ))
682
698
683
699
if self ._mode == "expand" :
684
700
mode = "expand"
@@ -908,7 +924,7 @@ def _find_best_position(self, width, height, renderer, consider=None):
908
924
renderer )
909
925
for x in range (1 , len (self .codes ))]
910
926
911
- # tx, ty = self.legendPatch.get_x(), self.legendPatch.get_y()
927
+ # tx, ty = self.legendPatch.get_x(), self.legendPatch.get_y()
912
928
913
929
candidates = []
914
930
for l , b in consider :
0 commit comments