Skip to content

Commit 869cc9d

Browse files
committed
Merge pull request #6110 from nansonzheng/1235-fix-proposal
Fixes #1235
1 parent ebfbc4a commit 869cc9d

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

lib/matplotlib/legend.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,7 @@ def _auto_legend_data(self):
737737
ax = self.parent
738738
bboxes = []
739739
lines = []
740+
offsets = []
740741

741742
for handle in ax.lines:
742743
assert isinstance(handle, Line2D)
@@ -755,12 +756,19 @@ def _auto_legend_data(self):
755756
transform = handle.get_transform()
756757
bboxes.append(handle.get_path().get_extents(transform))
757758

759+
for handle in ax.collections:
760+
transform, transOffset, hoffsets, paths = handle._prepare_points()
761+
762+
if len(hoffsets):
763+
for offset in transOffset.transform(hoffsets):
764+
offsets.append(offset)
765+
758766
try:
759767
vertices = np.concatenate([l.vertices for l in lines])
760768
except ValueError:
761769
vertices = np.array([])
762770

763-
return [vertices, bboxes, lines]
771+
return [vertices, bboxes, lines, offsets]
764772

765773
def draw_frame(self, b):
766774
'b is a boolean. Set draw frame to b'
@@ -920,7 +928,7 @@ def _find_best_position(self, width, height, renderer, consider=None):
920928
# should always hold because function is only called internally
921929
assert self.isaxes
922930

923-
verts, bboxes, lines = self._auto_legend_data()
931+
verts, bboxes, lines, offsets = self._auto_legend_data()
924932

925933
bbox = Bbox.from_bounds(0, 0, width, height)
926934
if consider is None:
@@ -939,6 +947,7 @@ def _find_best_position(self, width, height, renderer, consider=None):
939947
# take their into account when checking vertex overlaps in
940948
# the next line.
941949
badness = legendBox.count_contains(verts)
950+
badness += legendBox.count_contains(offsets)
942951
badness += legendBox.count_overlaps(bboxes)
943952
for line in lines:
944953
# FIXME: the following line is ill-suited for lines

lib/matplotlib/tests/test_legend.py

+27
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import matplotlib.pyplot as plt
1717
import matplotlib as mpl
1818
import matplotlib.patches as mpatches
19+
import matplotlib.transforms as mtrans
1920

2021

2122
@image_comparison(baseline_images=['legend_auto1'], remove_text=True)
@@ -261,6 +262,32 @@ def test_nanscatter():
261262
ax.grid(True)
262263

263264

265+
@image_comparison(baseline_images=['not_covering_scatter'], extensions=['png'])
266+
def test_not_covering_scatter():
267+
colors = ['b','g','r']
268+
269+
for n in range(3):
270+
plt.scatter([n,], [n,], color=colors[n])
271+
272+
plt.legend(['foo', 'foo', 'foo'], loc='best')
273+
plt.gca().set_xlim(-0.5, 2.2)
274+
plt.gca().set_ylim(-0.5, 2.2)
275+
276+
277+
@image_comparison(baseline_images=['not_covering_scatter_transform'],
278+
extensions=['png'])
279+
def test_not_covering_scatter_transform():
280+
# Offsets point to top left, the default auto position
281+
offset = mtrans.Affine2D().translate(-20, 20)
282+
x = np.linspace(0, 30, 1000)
283+
plt.plot(x, x)
284+
285+
plt.scatter([20], [10], transform=offset + plt.gca().transData)
286+
287+
plt.legend(['foo', 'bar'], loc='best')
288+
289+
264290
if __name__ == '__main__':
265291
import nose
266292
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)
293+

0 commit comments

Comments
 (0)