Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 48aa894

Browse files
committed
Check gridspecness of colorbars on the right figure.
... which is the figure on which the colorbar is actually added, and not necessarily the figure on which colorbar() is called.
1 parent efd66d4 commit 48aa894

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

lib/matplotlib/figure.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,32 +1271,36 @@ def colorbar(
12711271
if ax is None:
12721272
ax = getattr(mappable, "axes", None)
12731273

1274-
if (self.get_layout_engine() is not None and
1275-
not self.get_layout_engine().colorbar_gridspec):
1276-
use_gridspec = False
12771274
if cax is None:
12781275
if ax is None:
12791276
raise ValueError(
12801277
'Unable to determine Axes to steal space for Colorbar. '
12811278
'Either provide the *cax* argument to use as the Axes for '
12821279
'the Colorbar, provide the *ax* argument to steal space '
12831280
'from it, or add *mappable* to an Axes.')
1284-
current_ax = self.gca()
1281+
fig = ( # Figure of first axes; logic copied from make_axes.
1282+
[*ax.flat] if isinstance(ax, np.ndarray)
1283+
else [*ax] if np.iterable(ax)
1284+
else [ax])[0].figure
1285+
current_ax = fig.gca()
1286+
if (fig.get_layout_engine() is not None and
1287+
not fig.get_layout_engine().colorbar_gridspec):
1288+
use_gridspec = False
12851289
if (use_gridspec
12861290
and isinstance(ax, mpl.axes._base._AxesBase)
12871291
and ax.get_subplotspec()):
12881292
cax, kwargs = cbar.make_axes_gridspec(ax, **kwargs)
12891293
else:
12901294
cax, kwargs = cbar.make_axes(ax, **kwargs)
12911295
# make_axes calls add_{axes,subplot} which changes gca; undo that.
1292-
self.sca(current_ax)
1296+
fig.sca(current_ax)
12931297
cax.grid(visible=False, which='both', axis='both')
12941298

12951299
NON_COLORBAR_KEYS = [ # remove kws that cannot be passed to Colorbar
12961300
'fraction', 'pad', 'shrink', 'aspect', 'anchor', 'panchor']
12971301
cb = cbar.Colorbar(cax, mappable, **{
12981302
k: v for k, v in kwargs.items() if k not in NON_COLORBAR_KEYS})
1299-
self.stale = True
1303+
cax.figure.stale = True
13001304
return cb
13011305

13021306
def subplots_adjust(self, left=None, bottom=None, right=None, top=None,

lib/matplotlib/tests/test_colorbar.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,3 +1217,16 @@ def test_colorbar_axes_parmeters():
12171217
fig.colorbar(im, ax=(ax[0], ax[1]))
12181218
fig.colorbar(im, ax={i: _ax for i, _ax in enumerate(ax)}.values())
12191219
fig.draw_without_rendering()
1220+
1221+
1222+
def test_colorbar_wrong_figure():
1223+
# If we decide in the future to disallow calling colorbar() on the "wrong" figure,
1224+
# just delete this test.
1225+
fig_tl = plt.figure(layout="tight")
1226+
fig_cl = plt.figure(layout="constrained")
1227+
im = fig_cl.add_subplot().imshow([[0, 1]])
1228+
# Make sure this doesn't try to setup a gridspec-controlled colorbar on fig_cl,
1229+
# which would crash CL.
1230+
fig_tl.colorbar(im)
1231+
fig_tl.draw_without_rendering()
1232+
fig_cl.draw_without_rendering()

0 commit comments

Comments
 (0)