From 2d2425b1294c43bcbbc87a8a650b8254dc8958e1 Mon Sep 17 00:00:00 2001 From: Greg Lucas Date: Sat, 5 Mar 2022 19:16:12 -0700 Subject: [PATCH] FIX: Colorbars check for subplotspec attribute before using Update the colorbar get_subplotsped() to only access attributes after a check for it having the attribute, else return None. --- lib/matplotlib/colorbar.py | 8 +++----- lib/mpl_toolkits/tests/test_axes_grid1.py | 12 ++++++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) 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])