diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index b9cd3fe8204d..bd74386b53e5 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -263,12 +263,10 @@ def get_subplotspec(self): # make tight_layout happy.. ss = getattr(self._cbar.ax, 'get_subplotspec', None) if ss is None: - if self._orig_locator is None: + if not hasattr(self._orig_locator, "get_subplotspec"): return None - ss = self._orig_locator.get_subplotspec() - else: - ss = ss() - return ss + ss = self._orig_locator.get_subplotspec + return ss() @docstring.Substitution(_colormap_kw_doc) diff --git a/lib/mpl_toolkits/tests/test_axes_grid1.py b/lib/mpl_toolkits/tests/test_axes_grid1.py index ad2095684a55..abf85179645a 100644 --- a/lib/mpl_toolkits/tests/test_axes_grid1.py +++ b/lib/mpl_toolkits/tests/test_axes_grid1.py @@ -100,6 +100,18 @@ def test_axesgrid_colorbar_log_smoketest(): grid.cbar_axes[0].colorbar(im) +def test_inset_colorbar_tight_layout_smoketest(): + fig, ax = plt.subplots(1, 1) + pts = ax.scatter([0, 1], [0, 1], c=[1, 5]) + + cax = inset_axes(ax, width="3%", height="70%") + plt.colorbar(pts, cax=cax) + + with pytest.warns(UserWarning, match="This figure includes Axes"): + # Will warn, but not raise an error + plt.tight_layout() + + @image_comparison(['inset_locator.png'], style='default', remove_text=True) def test_inset_locator(): fig, ax = plt.subplots(figsize=[5, 4])