Skip to content

fix for #1235 #2857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
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
13 changes: 11 additions & 2 deletions lib/matplotlib/legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,7 @@ def _auto_legend_data(self):
ax = self.parent
bboxes = []
lines = []
points = []

for handle in ax.lines:
assert isinstance(handle, Line2D)
Expand All @@ -734,12 +735,15 @@ def _auto_legend_data(self):
transform = handle.get_transform()
bboxes.append(handle.get_path().get_extents(transform))

for handle in ax.collections:
points.append(handle._offsets)

try:
vertices = np.concatenate([l.vertices for l in lines])
except ValueError:
vertices = np.array([])

return [vertices, bboxes, lines]
return [vertices, bboxes, lines, points]

def draw_frame(self, b):
'b is a boolean. Set draw frame to b'
Expand Down Expand Up @@ -896,7 +900,7 @@ def _find_best_position(self, width, height, renderer, consider=None):
# should always hold because function is only called internally
assert self.isaxes

verts, bboxes, lines = self._auto_legend_data()
verts, bboxes, lines, points = self._auto_legend_data()

bbox = Bbox.from_bounds(0, 0, width, height)
if consider is None:
Expand Down Expand Up @@ -927,6 +931,11 @@ def _find_best_position(self, width, height, renderer, consider=None):
if line.intersects_bbox(legendBox):
badness += 1

for point in points:
val = self.parent.transData.transform(np.vstack(point[0]).T)
if legendBox.count_contains(val):
badness += 1

ox, oy = l, b
if badness == 0:
return ox, oy
Expand Down
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading