Skip to content

Commit 2c463fe

Browse files
authored
Merge pull request matplotlib#22611 from greglucas/axesgrid-inset-cbar
FIX: Colorbars check for subplotspec attribute before using
2 parents a4c6103 + 2d2425b commit 2c463fe

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

lib/matplotlib/colorbar.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,10 @@ def get_subplotspec(self):
263263
# make tight_layout happy..
264264
ss = getattr(self._cbar.ax, 'get_subplotspec', None)
265265
if ss is None:
266-
if self._orig_locator is None:
266+
if not hasattr(self._orig_locator, "get_subplotspec"):
267267
return None
268-
ss = self._orig_locator.get_subplotspec()
269-
else:
270-
ss = ss()
271-
return ss
268+
ss = self._orig_locator.get_subplotspec
269+
return ss()
272270

273271

274272
@docstring.Substitution(_colormap_kw_doc)

lib/mpl_toolkits/tests/test_axes_grid1.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,18 @@ def test_axesgrid_colorbar_log_smoketest():
100100
grid.cbar_axes[0].colorbar(im)
101101

102102

103+
def test_inset_colorbar_tight_layout_smoketest():
104+
fig, ax = plt.subplots(1, 1)
105+
pts = ax.scatter([0, 1], [0, 1], c=[1, 5])
106+
107+
cax = inset_axes(ax, width="3%", height="70%")
108+
plt.colorbar(pts, cax=cax)
109+
110+
with pytest.warns(UserWarning, match="This figure includes Axes"):
111+
# Will warn, but not raise an error
112+
plt.tight_layout()
113+
114+
103115
@image_comparison(['inset_locator.png'], style='default', remove_text=True)
104116
def test_inset_locator():
105117
fig, ax = plt.subplots(figsize=[5, 4])

0 commit comments

Comments
 (0)