@@ -1271,32 +1271,36 @@ def colorbar(
1271
1271
if ax is None :
1272
1272
ax = getattr (mappable , "axes" , None )
1273
1273
1274
- if (self .get_layout_engine () is not None and
1275
- not self .get_layout_engine ().colorbar_gridspec ):
1276
- use_gridspec = False
1277
1274
if cax is None :
1278
1275
if ax is None :
1279
1276
raise ValueError (
1280
1277
'Unable to determine Axes to steal space for Colorbar. '
1281
1278
'Either provide the *cax* argument to use as the Axes for '
1282
1279
'the Colorbar, provide the *ax* argument to steal space '
1283
1280
'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
1285
1289
if (use_gridspec
1286
1290
and isinstance (ax , mpl .axes ._base ._AxesBase )
1287
1291
and ax .get_subplotspec ()):
1288
1292
cax , kwargs = cbar .make_axes_gridspec (ax , ** kwargs )
1289
1293
else :
1290
1294
cax , kwargs = cbar .make_axes (ax , ** kwargs )
1291
1295
# make_axes calls add_{axes,subplot} which changes gca; undo that.
1292
- self .sca (current_ax )
1296
+ fig .sca (current_ax )
1293
1297
cax .grid (visible = False , which = 'both' , axis = 'both' )
1294
1298
1295
1299
NON_COLORBAR_KEYS = [ # remove kws that cannot be passed to Colorbar
1296
1300
'fraction' , 'pad' , 'shrink' , 'aspect' , 'anchor' , 'panchor' ]
1297
1301
cb = cbar .Colorbar (cax , mappable , ** {
1298
1302
k : v for k , v in kwargs .items () if k not in NON_COLORBAR_KEYS })
1299
- self .stale = True
1303
+ cax . figure .stale = True
1300
1304
return cb
1301
1305
1302
1306
def subplots_adjust (self , left = None , bottom = None , right = None , top = None ,
0 commit comments