10000 Merge pull request #16302 from anntzer/autolegenddata · matplotlib/matplotlib@ae5fd1d · GitHub
[go: up one dir, main page]

Skip to content

Commit ae5fd1d

Browse files
authored
Merge pull request #16302 from anntzer/autolegenddata
MNT: Cleanup Legend._auto_legend_data.
2 parents 5cb84e0 + 48ec134 commit ae5fd1d

File tree

1 file changed

+26
-51
lines changed

1 file changed

+26
-51
lines changed

lib/matplotlib/legend.py

Lines changed: 26 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -814,55 +814,31 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
814814

815815
def _auto_legend_data(self):
816816
"""
817-
Returns list of vertices and extents covered by the plot.
817+
Return display coordinates for hit testing for "best" positioning.
818818
819-
Returns a two long list.
820-
821-
First element is a list of (x, y) vertices (in
822-
display-coordinates) covered by all the lines and line
823-
collections, in the legend's handles.
824-
825-
Second element is a list of bounding boxes for all the patches in
826-
the legend's handles.
819+
Returns
820+
-------
821+
bboxes
822+
List of bounding boxes of all patches.
823+
lines
824+
List of `.Path` corresponding to each line.
825+
offsets
826+
List of (x, y) offsets of all collection.
827827
"""
828-
# should always hold because function is only called internally
829-
assert self.isaxes
830-
828+
assert self.isaxes # always holds, as this is only called internally
831829
ax = self.parent
832-
bboxes = []
833-
lines = []
830+
lines = [line.get_transform().transform_path(line.get_path())
831+
for line in ax.lines]
832+
bboxes = [patch.get_bbox().transformed(patch.get_data_transform())
833+
if isinstance(patch, Rectangle) else
834+
patch.get_path().get_extents(patch.get_transform())
835+
for patch in ax.patches]
834836
offsets = []
835-
836-
for handle in ax.lines:
837-
assert isinstance(handle, Line2D)
838-
path = handle.get_path()
839-
trans = handle.get_transform()
840-
tpath = trans.transform_path(path)
841-
lines.append(tpath)
842-
843-
for handle in ax.patches:
844-
assert isinstance(handle, Patch)
845-
846-
if isinstance(handle, Rectangle):
847-
transform = handle.get_data_transform()
848-
bboxes.append(handle.get_bbox().transformed(transform))
849-
else:
850-
transform = handle.get_transform()
851-
bboxes.append(handle.get_path().get_extents(transform))
852-
853837
for handle in ax.collections:
854-
transform, transOffset, hoffsets, paths = handle._prepare_points()
855-
856-
if len(hoffsets):
857-
for offset in transOffset.transform(hoffsets):
858-
offsets.append(offset)
859-
860-
try:
861-
vertices = np.concatenate([l.vertices for l in lines])
862-
except ValueError:
863-
vertices = np.array([])
864-
865-
return [vertices, bboxes, lines, offsets]
838+
_, transOffset, hoffsets, _ = handle._prepare_points()
839+
for offset in transOffset.transform(hoffsets):
840+
offsets.append(offset)
841+
return bboxes, lines, offsets
866842

867843
def get_children(self):
868844
# docstring inherited
@@ -1033,12 +1009,11 @@ def _find_best_position(self, width, height, renderer, consider=None):
10331009
*consider* is a list of ``(x, y)`` pairs to consider as a potential
10341010
lower-left corner of the legend. All are display coords.
10351011
"""
1036-
# should always hold because function is only called internally
1037-
assert self.isaxes
1012+
assert self.isaxes # always holds, as this is only called internally
10381013

10391014
start_time = time.perf_counter()
10401015

1041-
verts, bboxes, lines, offsets = self._auto_legend_data()
1016+
bboxes, lines, offsets = self._auto_legend_data()
10421017

10431018
bbox = Bbox.from_bounds(0, 0, width, height)
10441019
if consider is None:
@@ -1051,10 +1026,10 @@ def _find_best_position(self, width, height, renderer, consider=None):
10511026
for idx, (l, b) in enumerate(consider):
10521027
legendBox = Bbox.from_bounds(l, b, width, height)
10531028
badness = 0
1054-
# XXX TODO: If markers are present, it would be good to
1055-
# take them into account when checking vertex overlaps in
1056-
# the next line.
1057-
badness = (legendBox.count_contains(verts)
1029+
# XXX TODO: If markers are present, it would be good to take them
1030+
# into account when checking vertex overlaps in the next line.
1031+
badness = (sum(legendBox.count_contains(line.vertices)
1032+
for line in lines)
10581033
+ legendBox.count_contains(offsets)
10591034
+ legendBox.count_overlaps(bboxes)
10601035
+ sum(line.intersects_bbox(legendBox, filled=False)

0 commit comments

Comments
 (0)
0