From 7c2b45d962bcf8eda85329b8ce52796a4d9257fc Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Sun, 28 Oct 2018 13:00:26 -0700 Subject: [PATCH] Backport PR #12635: FIX: allow non bbox_extra_artists calls --- lib/matplotlib/figure.py | 13 ++++++++++--- lib/mpl_toolkits/axes_grid1/parasite_axes.py | 6 ++++-- lib/mpl_toolkits/tests/test_axes_grid1.py | 15 +++++++++++++++ 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 6259b297489c..ae64ce6034ed 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -2271,9 +2271,16 @@ def get_tightbbox(self, renderer, bbox_extra_artists=None): if bbox is not None and (bbox.width != 0 or bbox.height != 0): bb.append(bbox) - bb.extend( - ax.get_tightbbox(renderer, bbox_extra_artists=bbox_extra_artists) - for ax in self.axes if ax.get_visible()) + for ax in self.axes: + if ax.get_visible(): + # some axes don't take the bbox_extra_artists kwarg so we + # need this conditional.... + try: + bbox = ax.get_tightbbox(renderer, + bbox_extra_artists=bbox_extra_artists) + except TypeError: + bbox = ax.get_tightbbox(renderer) + bb.append(bbox) if len(bb) == 0: return self.bbox_inches diff --git a/lib/mpl_toolkits/axes_grid1/parasite_axes.py b/lib/mpl_toolkits/axes_grid1/parasite_axes.py index be3570f9b49a..42020e44a20d 100644 --- a/lib/mpl_toolkits/axes_grid1/parasite_axes.py +++ b/lib/mpl_toolkits/axes_grid1/parasite_axes.py @@ -328,11 +328,13 @@ def _remove_method(h): return ax2 - def get_tightbbox(self, renderer, call_axes_locator=True): + def get_tightbbox(self, renderer, call_axes_locator=True, + bbox_extra_artists=None): bbs = [ax.get_tightbbox(renderer, call_axes_locator=call_axes_locator) for ax in self.parasites] bbs.append(super().get_tightbbox(renderer, - call_axes_locator=call_axes_locator)) + call_axes_locator=call_axes_locator, + bbox_extra_artists=bbox_extra_artists)) return Bbox.union([b for b in bbs if b.width != 0 or b.height != 0]) diff --git a/lib/mpl_toolkits/tests/test_axes_grid1.py b/lib/mpl_toolkits/tests/test_axes_grid1.py index 2ac9d7d089fb..f1ededf67203 100644 --- a/lib/mpl_toolkits/tests/test_axes_grid1.py +++ b/lib/mpl_toolkits/tests/test_axes_grid1.py @@ -407,3 +407,18 @@ def test_image_grid(): for i in range(4): grid[i].imshow(im) grid[i].set_title('test {0}{0}'.format(i)) + + +def test_gettightbbox(): + + fig, ax = plt.subplots(figsize=(8, 6)) + + l, = ax.plot([1, 2, 3], [0, 1, 0]) + + ax_zoom = zoomed_inset_axes(ax, 4) + ax_zoom.plot([1, 2, 3], [0, 1, 0]) + + mark_inset(ax, ax_zoom, loc1=1, loc2=3, fc="none", ec='0.3') + bbox = fig.get_tightbbox(fig.canvas.get_renderer()) + np.testing.assert_array_almost_equal(bbox.extents, + [-18.022743, -14.118056, 7.332813, 5.4625])