Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ebc7b67

Browse filesBrowse files
committed
Dedupe some box anchoring code between legend.py and offsetbox.py.
1 parent 450cdc6 commit ebc7b67
Copy full SHA for ebc7b67

File tree

Expand file treeCollapse file tree

2 files changed

+26
-71
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+26
-71
lines changed

‎lib/matplotlib/legend.py

Copy file name to clipboardExpand all lines: lib/matplotlib/legend.py
+11-40Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import numpy as np
2929

3030
import matplotlib as mpl
31-
from matplotlib import _api, docstring, colors
31+
from matplotlib import _api, docstring, colors, offsetbox
3232
from matplotlib.artist import Artist, allow_rasterization
3333
from matplotlib.cbook import silent_list
3434
from matplotlib.font_manager import FontProperties
@@ -40,10 +40,11 @@
4040
PolyCollection, RegularPolyCollection)
4141
from matplotlib.transforms import Bbox, BboxBase, TransformedBbox
4242
from matplotlib.transforms import BboxTransformTo, BboxTransformFrom
43-
44-
from matplotlib.offsetbox import HPacker, VPacker, TextArea, DrawingArea
45-
from matplotlib.offsetbox import DraggableOffsetBox
46-
43+
from matplotlib.offsetbox import (
44+
AnchoredOffsetbox, DraggableOffsetBox,
45+
HPacker, VPacker,
46+
DrawingArea, TextArea,
47+
)
4748
from matplotlib.container import ErrorbarContainer, BarContainer, StemContainer
4849
from . import legend_handler
4950

@@ -280,21 +281,10 @@ def _update_bbox_to_anchor(self, loc_in_canvas):
280281
class Legend(Artist):
281282
"""
282283
Place a legend on the axes at location loc.
283-
284284
"""
285-
codes = {'best': 0, # only implemented for axes legends
286-
'upper right': 1,
287-
'upper left': 2,
288-
'lower left': 3,
289-
'lower right': 4,
290-
'right': 5,
291-
'center left': 6,
292-
'center right': 7,
293-
'lower center': 8,
294-
'upper center': 9,
295-
'center': 10,
296-
}
297285

286+
# 'best' is only implemented for axes legends
287+
codes = {'best': 0, **AnchoredOffsetbox.codes}
298288
zorder = 5
299289

300290
def __str__(self):
@@ -1014,29 +1004,10 @@ def _get_anchored_bbox(self, loc, bbox, parentbbox, renderer):
10141004
bbox to be placed, in display coordinates.
10151005
parentbbox : `~matplotlib.transforms.Bbox`
10161006
A parent box which will contain the bbox, in display coordinates.
1017-
10181007
"""
1019-
assert loc in range(1, 11) # called only internally
1020-
1021-
BEST, UR, UL, LL, LR, R, CL, CR, LC, UC, C = range(11)
1022-
1023-
anchor_coefs = {UR: "NE",
1024-
UL: "NW",
1025-
LL: "SW",
1026-
LR: "SE",
1027-
R: "E",
1028-
CL: "W",
1029-
CR: "E",
1030-
LC: "S",
1031-
UC: "N",
1032-
C: "C"}
1033-
1034-
c = anchor_coefs[loc]
1035-
1036-
fontsize = renderer.points_to_pixels(self._fontsize)
1037-
container = parentbbox.padded(-self.borderaxespad * fontsize)
1038-
anchored_box = bbox.anchored(c, container=container)
1039-
return anchored_box.x0, anchored_box.y0
1008+
return offsetbox._get_anchored_bbox(
1009+
loc, bbox, parentbbox,
1010+
self.borderaxespad * renderer.points_to_pixels(self._fontsize))
10401011

10411012
def _find_best_position(self, width, height, renderer, consider=None):
10421013
"""

‎lib/matplotlib/offsetbox.py

Copy file name to clipboardExpand all lines: lib/matplotlib/offsetbox.py
+15-31Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,17 +1098,14 @@ def _update_offset_func(self, renderer, fontsize=None):
10981098
"""
10991099
if fontsize is None:
11001100
fontsize = renderer.points_to_pixels(
1101-
self.prop.get_size_in_points())
1101+
self.prop.get_size_in_points())
11021102

1103-
def _offset(w, h, xd, yd, renderer, fontsize=fontsize, self=self):
1103+
def _offset(w, h, xd, yd, renderer):
11041104
bbox = Bbox.from_bounds(0, 0, w, h)
11051105
borderpad = self.borderpad * fontsize
11061106
bbox_to_anchor = self.get_bbox_to_anchor()
1107-
1108-
x0, y0 = self._get_anchored_bbox(self.loc,
1109-
bbox,
1110-
bbox_to_anchor,
1111-
borderpad)
1107+
x0, y0 = _get_anchored_bbox(
1108+
self.loc, bbox, bbox_to_anchor, borderpad)
11121109
return x0 + xd, y0 + yd
11131110

11141111
self.set_offset(_offset)
@@ -1139,31 +1136,18 @@ def draw(self, renderer):
11391136
self.get_child().draw(renderer)
11401137
self.stale = False
11411138

1142-
def _get_anchored_bbox(self, loc, bbox, parentbbox, borderpad):
1143-
"""
1144-
Return the position of the bbox anchored at the parentbbox
1145-
with the loc code, with the borderpad.
1146-
"""
1147-
assert loc in range(1, 11) # called only internally
1148-
1149-
BEST, UR, UL, LL, LR, R, CL, CR, LC, UC, C = range(11)
11501139

1151-
anchor_coefs = {UR: "NE",
1152-
UL: "NW",
1153-
LL: "SW",
1154-
LR: "SE",
1155-
R: "E",
1156-
CL: "W",
1157-
CR: "E",
1158-
LC: "S",
1159-
UC: "N",
1160-
C: "C"}
1161-
1162-
c = anchor_coefs[loc]
1163-
1164-
container = parentbbox.padded(-borderpad)
1165-
anchored_box = bbox.anchored(c, container=container)
1166-
return anchored_box.x0, anchored_box.y0
1140+
def _get_anchored_bbox(loc, bbox, parentbbox, borderpad):
1141+
"""
1142+
Return the (x, y) position of the *bbox* anchored at the *parentbbox* with
1143+
the *loc* code with the *borderpad*.
1144+
"""
1145+
# This is only called internally and *loc* should already have been
1146+
# validated. If 0 (None), we just let ``bbox.anchored`` raise.
1147+
c = [None, "NE", "NW", "SW", "SE", "E", "W", "E", "S", "N", "C"][loc]
1148+
container = parentbbox.padded(-borderpad)
1149+
anchored_box = bbox.anchored(c, container=container)
1150+
return anchored_box.x0, anchored_box.y0
11671151

11681152

11691153
class AnchoredText(AnchoredOffsetbox):

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.