diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 846e112bf5dc..cc3995247e93 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -1753,7 +1753,10 @@ def legend(self, *args, **kwargs): # kwargs['loc'] = extra_args[0] # extra_args = extra_args[1:] pass - l = mlegend.Legend(self, handles, labels, *extra_args, **kwargs) + transform = kwargs.pop('bbox_transform', self.transFigure) + # explicitly set the bbox transform if the user hasn't. + l = mlegend.Legend(self, handles, labels, *extra_args, + bbox_transform=transform, **kwargs) self.legends.append(l) l._remove_method = self.legends.remove self.stale = True diff --git a/lib/matplotlib/tests/baseline_images/test_bbox_tight/bbox_inches_tight.pdf b/lib/matplotlib/tests/baseline_images/test_bbox_tight/bbox_inches_tight.pdf index fbf29531c266..9214e27e528c 100644 Binary files a/lib/matplotlib/tests/baseline_images/test_bbox_tight/bbox_inches_tight.pdf and b/lib/matplotlib/tests/baseline_images/test_bbox_tight/bbox_inches_tight.pdf differ diff --git a/lib/matplotlib/tests/baseline_images/test_bbox_tight/bbox_inches_tight.png b/lib/matplotlib/tests/baseline_images/test_bbox_tight/bbox_inches_tight.png index 3aff36a6b054..f2827b5b78ea 100644 Binary files a/lib/matplotlib/tests/baseline_images/test_bbox_tight/bbox_inches_tight.png and b/lib/matplotlib/tests/baseline_images/test_bbox_tight/bbox_inches_tight.png differ diff --git a/lib/matplotlib/tests/baseline_images/test_bbox_tight/bbox_inches_tight.svg b/lib/matplotlib/tests/baseline_images/test_bbox_tight/bbox_inches_tight.svg index f0773ca06819..1e6229adef8a 100644 --- a/lib/matplotlib/tests/baseline_images/test_bbox_tight/bbox_inches_tight.svg +++ b/lib/matplotlib/tests/baseline_images/test_bbox_tight/bbox_inches_tight.svg @@ -1,8 +1,8 @@ - - + + diff --git a/lib/matplotlib/tests/test_bbox_tight.py b/lib/matplotlib/tests/test_bbox_tight.py index d4b3d6f1564d..4d52580e8b5d 100644 --- a/lib/matplotlib/tests/test_bbox_tight.py +++ b/lib/matplotlib/tests/test_bbox_tight.py @@ -10,7 +10,7 @@ @image_comparison(['bbox_inches_tight'], remove_text=True, - savefig_kwarg=dict(bbox_inches='tight')) + savefig_kwarg={'bbox_inches': 'tight'}) def test_bbox_inches_tight(): #: Test that a figure saved using bbox_inches='tight' is clipped correctly data = [[66386, 174296, 75131, 577908, 32015], @@ -35,6 +35,7 @@ def test_bbox_inches_tight(): plt.xticks([]) plt.xlim(0, 5) plt.legend([''] * 5, loc=(1.2, 0.2)) + fig.legend([''] * 5, bbox_to_anchor=(0, 0.2), loc='lower left') # Add a table at the bottom of the axes cellText.reverse() plt.table(cellText=cellText, rowLabels=rowLabels, colLabels=colLabels, diff --git a/lib/matplotlib/tests/test_legend.py b/lib/matplotlib/tests/test_legend.py index 9b528cae9811..df09a884b42c 100644 --- a/lib/matplotlib/tests/test_legend.py +++ b/lib/matplotlib/tests/test_legend.py @@ -309,28 +309,32 @@ def test_legend_handle_label(self): lines = ax.plot(range(10)) with mock.patch('matplotlib.legend.Legend') as Legend: fig.legend(lines, ['hello world']) - Legend.assert_called_with(fig, lines, ['hello world']) + Legend.assert_called_with(fig, lines, ['hello world'], + bbox_transform=fig.transFigure) def test_legend_no_args(self): fig, ax = plt.subplots() lines = ax.plot(range(10), label='hello world') with mock.patch('matplotlib.legend.Legend') as Legend: fig.legend() - Legend.assert_called_with(fig, lines, ['hello world']) + Legend.assert_called_with(fig, lines, ['hello world'], + bbox_transform=fig.transFigure) def test_legend_label_arg(self): fig, ax = plt.subplots() lines = ax.plot(range(10)) with mock.patch('matplotlib.legend.Legend') as Legend: fig.legend(['foobar']) - Legend.assert_called_with(fig, lines, ['foobar']) + Legend.assert_called_with(fig, lines, ['foobar'], + bbox_transform=fig.transFigure) def test_legend_label_three_args(self): fig, ax = plt.subplots() lines = ax.plot(range(10)) with mock.patch('matplotlib.legend.Legend') as Legend: fig.legend(lines, ['foobar'], 'right') - Legend.assert_called_with(fig, lines, ['foobar'], 'right') + Legend.assert_called_with(fig, lines, ['foobar'], 'right', + bbox_transform=fig.transFigure) def test_legend_label_three_args_pluskw(self): # test that third argument and loc= called together give @@ -347,7 +351,8 @@ def test_legend_kw_args(self): with mock.patch('matplotlib.legend.Legend') as Legend: fig.legend(loc='right', labels=('a', 'b'), handles=(lines, lines2)) Legend.assert_called_with( - fig, (lines, lines2), ('a', 'b'), loc='right') + fig, (lines, lines2), ('a', 'b'), loc='right', + bbox_transform=fig.transFigure) def test_warn_args_kwargs(self): fig, axs = plt.subplots(1, 2)