Skip to content

Commit d1bac23

Browse files
committed
ENH: Added new kwarg to legend to control the placement of legend markers and labels. One now has the ability to place the marker to the right of the label.
1 parent 68176eb commit d1bac23

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,10 @@ def legend(self, *args, **kwargs):
360360
drawn ones. Default is ``None`` which will take the value from
361361
the ``legend.markerscale`` :data:`rcParam <matplotlib.rcParams>`.
362362
363+
*markerfirst*: [ *True* | *False* ]
364+
if *True*, legend marker is placed to the left of the legend label
365+
if *False*, legend marker is placed to the right of the legend label
366+
363367
frameon : None or bool
364368
Control whether a frame should be drawn around the legend.
365369
Default is ``None`` which will take the value from the

lib/matplotlib/figure.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,10 @@ def legend(self, handles, labels, *args, **kwargs):
11481148
The relative size of legend markers vs. original. If *None*, use rc
11491149
settings.
11501150
1151+
*markerfirst*: [ *True* | *False* ]
1152+
if *True*, legend marker is placed to the left of the legend label
1153+
if *False*, legend marker is placed to the right of the legend label
1154+
11511155
*fancybox*: [ *None* | *False* | *True* ]
11521156
if *True*, draw a frame with a round fancybox. If *None*, use rc
11531157

lib/matplotlib/legend.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)