diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index 928f52c3b8a6..cbac68429982 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -716,6 +716,7 @@ def _auto_legend_data(self): ax = self.parent bboxes = [] lines = [] + points = [] for handle in ax.lines: assert isinstance(handle, Line2D) @@ -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' @@ -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: @@ -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 diff --git a/lib/matplotlib/tests/baseline_images/test_legend/legend_best1.pdf b/lib/matplotlib/tests/baseline_images/test_legend/legend_best1.pdf new file mode 100644 index 000000000000..36d717330e1a Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_legend/legend_best1.pdf differ diff --git a/lib/matplotlib/tests/baseline_images/test_legend/legend_best1.png b/lib/matplotlib/tests/baseline_images/test_legend/legend_best1.png new file mode 100644 index 000000000000..71e5190db944 Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_legend/legend_best1.png differ diff --git a/lib/matplotlib/tests/baseline_images/test_legend/legend_best1.svg b/lib/matplotlib/tests/baseline_images/test_legend/legend_best1.svg new file mode 100644 index 000000000000..b1685a27d5ff --- /dev/null +++ b/lib/matplotlib/tests/baseline_images/test_legend/legend_best1.svg @@ -0,0 +1,603 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/lib/matplotlib/tests/baseline_images/test_legend/legend_best2.pdf b/lib/matplotlib/tests/baseline_images/test_legend/legend_best2.pdf new file mode 100644 index 000000000000..0497a5760219 Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_legend/legend_best2.pdf differ diff --git a/lib/matplotlib/tests/baseline_images/test_legend/legend_best2.png b/lib/matplotlib/tests/baseline_images/test_legend/legend_best2.png new file mode 100644 index 000000000000..cb8631c20635 Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_legend/legend_best2.png differ diff --git a/lib/matplotlib/tests/baseline_images/test_legend/legend_best2.svg b/lib/matplotlib/tests/baseline_images/test_legend/legend_best2.svg new file mode 100644 index 000000000000..c4b37c43db3f --- /dev/null +++ b/lib/matplotlib/tests/baseline_images/test_legend/legend_best2.svg @@ -0,0 +1,659 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/lib/matplotlib/tests/test_legend.py b/lib/matplotlib/tests/test_legend.py index 4fda269d853a..86e8ee0d11d9 100644 --- a/lib/matplotlib/tests/test_legend.py +++ b/lib/matplotlib/tests/test_legend.py @@ -50,6 +50,29 @@ def test_legend_auto3(): ax.legend(loc=0) +@image_comparison(baseline_images=['legend_best1']) +def test_legend_best1(): + 'Test best automatic legend placement' + fig = plt.figure() + ax = fig.add_subplot(111) + ax.scatter(0.95, 0.95) + ax.set_xlim(0.0, 1.0) + ax.set_ylim(0.0, 1.0) + ax.legend(['foo'],loc='best') + + +@image_comparison(baseline_images=['legend_best2']) +def test_legend_best2(): + 'Test best automatic legend placement' + fig = plt.figure() + ax = fig.add_subplot(111) + ax.scatter(0.95, 0.95) + ax.scatter(0.05, 0.95) + ax.set_xlim(0.0, 1.0) + ax.set_ylim(0.0, 1.0) + ax.legend(['foo', 'foo'],loc='best') + + @image_comparison(baseline_images=['legend_various_labels'], remove_text=True) def test_various_labels(): # tests all sorts of label types