Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f052d1e

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 af4ab53 commit f052d1e

1 file changed

Lines changed: 26 additions & 49 deletions

File tree

lib/matplotlib/legend.py

Lines changed: 26 additions & 49 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:
864-
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]
851+
_, transOffset, hoffsets, _ = handle._prepare_points()
852+
for offset in transOffset.transform(hoffsets):
853+
offsets.append(offset)
854+
return vertices, bboxes, lines, offsets
876855

877856
def get_children(self):
878857
"""Return the list of child artists."""
@@ -1048,8 +1027,7 @@ def _find_best_position(self, width, height, renderer, consider=None):
10481027
*consider* is a list of ``(x, y)`` pairs to consider as a potential
10491028
lower-left corner of the legend. All are display coords.
10501029
"""
1051-
# should always hold because function is only called internally
1052-
assert self.isaxes
1030+
assert self.isaxes # always holds, as this is only called internally
10531031

10541032
start_time = time.perf_counter()
10551033

@@ -1066,9 +1044,8 @@ def _find_best_position(self, width, height, renderer, consider=None):
10661044
for idx, (l, b) in enumerate(consider):
10671045
legendBox = Bbox.from_bounds(l, b, width, height)
10681046
badness = 0
1069-
# XXX TODO: If markers are present, it would be good to
1070-
# take them into account when checking vertex overlaps in
1071-
# the next line.
1047+
# XXX TODO: If markers are present, it would be good to take them
1048+
# into account when checking vertex overlaps in the next line.
10721049
badness = (legendBox.count_contains(verts)
10731050
+ legendBox.count_contains(offsets)
10741051
+ legendBox.count_overlaps(bboxes)

0 commit comments

Comments
 (0)