Skip to content

Commit

Permalink
fix for matplotlib#1235
Browse files Browse the repository at this point in the history
  • Loading branch information
wmak committed Mar 3, 2014
1 parent b5a6c49 commit f6a28ba
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/matplotlib/legend.py
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

0 comments on commit f6a28ba

Please sign in to comment.