8000 ENH: add outside argument to axes legend as well · matplotlib/matplotlib@22a3c37 · GitHub
[go: up one dir, main page]

Skip to content

Commit 22a3c37

Browse files
committed
ENH: add outside argument to axes legend as well
1 parent c51bb2f commit 22a3c37

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

lib/matplotlib/legend.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ def __init__(self, parent, handles, labels,
368368
bbox_transform=None, # transform for the bbox
369369
frameon=None, # draw frame
370370
handler_map=None,
371+
outside=False,
371372
):
372373
"""
373374
Parameters
@@ -425,6 +426,7 @@ def __init__(self, parent, handles, labels,
425426
self.legendHandles = []
426427
self._legend_title_box = None
427428

429+
self.outside = outside
428430
#: A dictionary with the extra handler mappings for this Legend
429431
#: instance.
430432
self._custom_handler_map = handler_map
@@ -1106,8 +1108,37 @@ def _get_anchored_bbox(self, loc, bbox, parentbbox, renderer):
11061108
c = anchor_coefs[loc]
11071109

11081110
fontsize = renderer.points_to_pixels(self._fontsize)
1109-
container = parentbbox.padded(-(self.borderaxespad) * fontsize)
1110-
anchored_box = bbox.anchored(c, container=container)
1111+
if not self.outside:
1112+
container = parentbbox.padded(-(self.borderaxespad) * fontsize)
1113+
anchored_box = bbox.anchored(c, container=container)
1114+
else:
1115+
if c in ['NE', 'SE', 'E']:
1116+
stack = 'right'
1117+
elif c in ['NW', 'SW', 'W']:
1118+
stack = 'left'
1119+
elif c in ['N']:
1120+
stack = 'top'
1121+
else:
1122+
stack = 'bottom'
1123+
if self.outside == 'vertical':
1124+
if c in ['NE', 'NW']:
1125+
stack = 'top'
1126+
elif c in ['SE', 'SW']:
1127+
stack = 'bottom'
1128+
anchored_box = bbox.anchored(c, container=parentbbox)
1129+
if stack == 'right':
1130+
anchored_box.x0 = (anchored_box.x0 + anchored_box.width +
1131+
(self.borderaxespad) * fontsize)
1132+
elif stack == 'left':
1133+
anchored_box.x0 = (anchored_box.x0 - anchored_b 890E ox.width -
1134+
(self.borderaxespad) * fontsize)
1135+
elif stack == 'bottom':
1136+
anchored_box.y0 = (anchored_box.y0 - anchored_box.height -
1137+
(self.borderaxespad) * fontsize)
1138+
elif stack == 'top':
1139+
anchored_box.y0 = (anchored_box.y0 + anchored_box.height +
1140+
(self.borderaxespad) * fontsize)
1141+
11111142
return anchored_box.x0, anchored_box.y0
11121143

11131144
def _find_best_position(self, width, height, renderer, consider=None):

0 commit comments

Comments
 (0)
0