@@ -1279,32 +1279,36 @@ def colorbar(
1279
1279
if ax is None :
1280
1280
ax = getattr (mappable , "axes" , None )
1281
1281
1282
- if (self .get_layout_engine () is not None and
1283
- not self .get_layout_engine ().colorbar_gridspec ):
1284
- use_gridspec = False
1285
1282
if cax is None :
1286
1283
if ax is None :
1287
1284
raise ValueError (
1288
1285
'Unable to determine Axes to steal space for Colorbar. '
1289
1286
'Either provide the *cax* argument to use as the Axes for '
1290
1287
'the Colorbar, provide the *ax* argument to steal space '
1291
1288
'from it, or add *mappable* to an Axes.' )
1292
- current_ax = self .gca ()
1289
+ fig = ( # Figure of first axes; logic copied from make_axes.
1290
+ [* ax .flat ] if isinstance (ax , np .ndarray )
1291
+ else [* ax ] if np .iterable (ax )
1292
+ else [ax ])[0 ].figure
1293
+ current_ax = fig .gca ()
1294
+ if (fig .get_layout_engine () is not None and
1295
+ not fig .get_layout_engine ().colorbar_gridspec ):
1296
+ use_gridspec = False
1293
1297
if (use_gridspec
1294
1298
and isinstance (ax , mpl .axes ._base ._AxesBase )
1295
1299
and ax .get_subplotspec ()):
1296
1300
cax , kwargs = cbar .make_axes_gridspec (ax , ** kwargs )
1297
1301
else :
1298
1302
cax , kwargs = cbar .make_axes (ax , ** kwargs )
1299
1303
# make_axes calls add_{axes,subplot} which changes gca; undo that.
1300
- self .sca (current_ax )
1304
+ fig .sca (current_ax )
1301
1305
cax .grid (visible = False , which = 'both' , axis = 'both' )
1302
1306
1303
1307
NON_COLORBAR_KEYS = [ # remove kws that cannot be passed to Colorbar
1304
1308
'fraction' , 'pad' , 'shrink' , 'aspect' , 'anchor' , 'panchor' ]
1305
1309
cb = cbar .Colorbar (cax , mappable , ** {
1306
1310
k : v for k , v in kwargs .items () if k not in NON_COLORBAR_KEYS })
1307
- self .stale = True
1311
+ cax . figure .stale = True
1308
1312
return cb
1309
1313
1310
1314
def subplots_adjust (self , left = None , bottom = None , right = None , top = None ,
0 commit comments