Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 11 additions & 40 deletions lib/matplotlib/legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import numpy as np

import matplotlib as mpl
from matplotlib import _api, docstring, colors
from matplotlib import _api, docstring, colors, offsetbox
from matplotlib.artist import Artist, allow_rasterization
from matplotlib.cbook import silent_list
from matplotlib.font_manager import FontProperties
Expand All @@ -40,10 +40,11 @@
PolyCollection, RegularPolyCollection)
from matplotlib.transforms import Bbox, BboxBase, TransformedBbox
from matplotlib.transforms import BboxTransformTo, BboxTransformFrom

from matplotlib.offsetbox import HPacker, VPacker, TextArea, DrawingArea
from matplotlib.offsetbox import DraggableOffsetBox

from matplotlib.offsetbox import (
AnchoredOffsetbox, DraggableOffsetBox,
HPacker, VPacker,
DrawingArea, TextArea,
)
from matplotlib.container import ErrorbarContainer, BarContainer, StemContainer
from . import legend_handler

Expand Down Expand Up @@ -280,21 +281,10 @@ def _update_bbox_to_anchor(self, loc_in_canvas):
class Legend(Artist):
"""
Place a legend on the axes at location loc.

"""
codes = {'best': 0, # only implemented for axes legends
'upper right': 1,
'upper left': 2,
'lower left': 3,
'lower right': 4,
'right': 5,
'center left': 6,
'center right': 7,
'lower center': 8,
'upper center': 9,
'center': 10,
}

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

def __str__(self):
Expand Down Expand Up @@ -1014,29 +1004,10 @@ def _get_anchored_bbox(self, loc, bbox, parentbbox, renderer):
bbox to be placed, in display coordinates.
parentbbox : `~matplotlib.transforms.Bbox`
A parent box which will contain the bbox, in display coordinates.

"""
assert loc in range(1, 11) # called only internally

BEST, UR, UL, LL, LR, R, CL, CR, LC, UC, C = range(11)

anchor_coefs = {UR: "NE",
UL: "NW",
LL: "SW",
LR: "SE",
R: "E",
CL: "W",
CR: "E",
LC: "S",
UC: "N",
C: "C"}

c = anchor_coefs[loc]

fontsize = renderer.points_to_pixels(self._fontsize)
container = parentbbox.padded(-self.borderaxespad * fontsize)
anchored_box = bbox.anchored(c, container=container)
return anchored_box.x0, anchored_box.y0
return offsetbox._get_anchored_bbox(
loc, bbox, parentbbox,
self.borderaxespad * renderer.points_to_pixels(self._fontsize))

def _find_best_position(self, width, height, renderer, consider=None):
"""
Expand Down
46 changes: 15 additions & 31 deletions lib/matplotlib/offsetbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -1098,17 +1098,14 @@ def _update_offset_func(self, renderer, fontsize=None):
"""
if fontsize is None:
fontsize = renderer.points_to_pixels(
self.prop.get_size_in_points())
self.prop.get_size_in_points())

def _offset(w, h, xd, yd, renderer, fontsize=fontsize, self=self):
def _offset(w, h, xd, yd, renderer):
bbox = Bbox.from_bounds(0, 0, w, h)
borderpad = self.borderpad * fontsize
bbox_to_anchor = self.get_bbox_to_anchor()

x0, y0 = self._get_anchored_bbox(self.loc,
bbox,
bbox_to_anchor,
borderpad)
x0, y0 = _get_anchored_bbox(
self.loc, bbox, bbox_to_anchor, borderpad)
return x0 + xd, y0 + yd

self.set_offset(_offset)
Expand Down Expand Up @@ -1139,31 +1136,18 @@ def draw(self, renderer):
self.get_child().draw(renderer)
self.stale = False

def _get_anchored_bbox(self, loc, bbox, parentbbox, borderpad):
"""
Return the position of the bbox anchored at the parentbbox
with the loc code, with the borderpad.
"""
assert loc in range(1, 11) # called only internally

BEST, UR, UL, LL, LR, R, CL, CR, LC, UC, C = range(11)

anchor_coefs = {UR: "NE",
UL: "NW",
LL: "SW",
LR: "SE",
R: "E",
CL: "W",
CR: "E",
LC: "S",
UC: "N",
C: "C"}

c = anchor_coefs[loc]

container = parentbbox.padded(-borderpad)
anchored_box = bbox.anchored(c, container=container)
return anchored_box.x0, anchored_box.y0
def _get_anchored_bbox(loc, bbox, parentbbox, borderpad):
"""
Return the (x, y) position of the *bbox* anchored at the *parentbbox* with
the *loc* code with the *borderpad*.
"""
# This is only called internally and *loc* should already have been
# validated. If 0 (None), we just let ``bbox.anchored`` raise.
c = [None, "NE", "NW", "SW", "SE", "E", "W", "E", "S", "N", "C"][loc]
container = parentbbox.padded(-borderpad)
anchored_box = bbox.anchored(c, container=container)
return anchored_box.x0, anchored_box.y0


class AnchoredText(AnchoredOffsetbox):
Expand Down