8000 Cleanup Legend._auto_legend_data. · matplotlib/matplotlib@c6a73b3 · GitHub
[go: up one dir, main page]

Skip to content

Commit c6a73b3

Browse files
committed
Cleanup Legend._auto_legend_data.
Fix the docstring, which was wrong. Use shorter list comprehensions to construct `vertices` and `bboxes`. Clarify the failure mode of `np.concatenate`. Remove an unnecessary check when constructing `offsets`.
1 parent 47cfa35 commit c6a73b3
File tree

1 file changed

+25
-48
lines changed

1 file changed

+25
-48
lines changed

lib/matplotlib/legend.py

Lines changed: 25 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -824,55 +824,34 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
824824

825825
def _auto_legend_data(self):
826826
"""
827-
Returns list of vertices and extents covered by the plot.
827+
Return display coordinates for hit testing for "best" positioning.
828828
829-
Returns a two long list.
830-
831-
First element is a list of (x, y) vertices (in
832-
display-coordinates) covered by all the lines and line
833-
collections, in the legend's handles.
834-
835-
Second element is a list of bounding boxes for all the patches in
836-
the legend's handles.
829+
Returns
830+
-------
831+
vertices
832+
List of (x, y) vertices of all lines.
833+
bboxes
834+
List of bounding boxes of all patches.
835+
lines
836+
List of `.Path` corresponding to each line.
837+
offsets
838+
List of (x, y) offsets of all collection.
837839
"""
838-
# should always hold because function is only called internally
839-
assert self.isaxes
840-
840+
assert self.isaxes # always holds, as this is only called internally
841841
ax = self.parent
842-
bboxes = []
843-
lines = []
842+
lines = [line.get_transform().transform_path(line.get_path())
843+
for line in ax.lines]
844+
vertices = np.concatenate([l.vertices for l in lines]) if lines else []
845+
bboxes = [patch.get_bbox().transformed(patch.get_data_transform())
846+
if isinstance(patch, Rectangle) else
847+
patch.get_path().get_extents(patch.get_transform())
848+
for patch in ax.patches]
844849
offsets = []
845-
846-
for handle in ax.lines:
847-
assert isinstance(handle, Line2D)
848-
path = handle.get_path()
849-
trans = handle.get_transform()
850-
tpath = trans.transform_path(path)
851-
lines.append(tpath)
852-
853-
for handle in ax.patches:
854-
assert isinstance(handle, Patch)
855-
856-
if isinstance(handle, Rectangle):
857-
transform = handle.get_data_transform()
858-
bboxes.append(handle.get_bbox().transformed(transform))
859-
else:
860-
transform = handle.get_transform()
861-
bboxes.append(handle.get_path().get_extents(transform))
862-
863850
for handle in ax.collections:
864851
transform, transOffset, hoffsets, paths = handle._prepare_points()
865-
866-
if len(hoffsets):
867-
for offset in transOffset.transform(hoffsets):
868-
offsets.append(offset)
869-
870-
try:
871-
vertices = np.concatenate([l.vertices for l in lines])
872-
except ValueError:
873-
vertices = np.array([])
874-
875-
return [vertices, bboxes, lines, offsets]
852+
for offset in transOffset.transform(hoffsets):
853+
offsets.append(offset)
854+
return (vertices, bboxes, lines, offsets)
876855

877856
def draw_frame(self, b):
878857
"""
@@ -1056,8 +1035,7 @@ def _find_best_position(self, width, height, renderer, consider=None):
10561035
*consider* is a list of ``(x, y)`` pairs to consider as a potential
10571036
lower-left corner of the legend. All are display coords.
10581037
"""
1059-
# should always hold because function is only called internally
1060-
assert self.isaxes
1038+
assert self.isaxes # always holds, as this is only called internally
10611039

10621040
start_time = time.perf_counter()
10631041

@@ -1074,9 +1052,8 @@ def _find_best_position(self, width, height, renderer, consider=None):
10741052
for idx, (l, b) in enumerate(consider):
10751053
legendBox = Bbox.from_bounds(l, b, width, height)
10761054
badness = 0
1077-
# XXX TODO: If markers are present, it would be good to
1078-
# take them into account when checking vertex overlaps in
1079-
# the next line.
1055+
# XXX TODO: If markers are present, it would be good to take them
1056+
# into account when checking vertex overlaps in the next line.
10801057
badness = (legendBox.count_contains(verts)
10811058
+ legendBox.count_contains(offsets)
10821059
+ legendBox.count_overlaps(bboxes)

0 commit comments

Comments
 (0)
0