@@ -1221,13 +1221,13 @@ def colorbar(
1221
1221
1222
1222
The *shrink* kwarg provides a simple way to scale the colorbar with
1223
1223
respect to the axes. Note that if *cax* is specified, it determines the
1224
- size of the colorbar and *shrink* and *aspect* kwargs are ignored.
1224
+ size of the colorbar, and *shrink* and *aspect* are ignored.
1225
1225
1226
1226
For more precise control, you can manually specify the positions of the
1227
1227
axes objects in which the mappable and the colorbar are drawn. In this
1228
1228
case, do not use any of the axes properties kwargs.
1229
1229
1230
- It is known that some vector graphics viewers (svg and pdf) renders
1230
+ It is known that some vector graphics viewers (svg and pdf) render
1231
1231
white gaps between segments of the colorbar. This is due to bugs in
1232
1232
the viewers, not Matplotlib. As a workaround, the colorbar can be
1233
1233
rendered with overlapping segments::
@@ -1247,37 +1247,30 @@ def colorbar(
1247
1247
if (self .get_layout_engine () is not None and
1248
1248
not self .get_layout_engine ().colorbar_gridspec ):
1249
1249
use_gridspec = False
1250
- # Store the value of gca so that we can set it back later on.
1251
1250
if cax is None :
1251
+ current_ax = self .gca ()
1252
1252
if ax is None :
1253
1253
_api .warn_deprecated ("3.6" , message = (
1254
1254
'Unable to determine Axes to steal space for Colorbar. '
1255
1255
'Using gca(), but will raise in the future. '
1256
1256
'Either provide the *cax* argument to use as the Axes for '
1257
1257
'the Colorbar, provide the *ax* argument to steal space '
1258
1258
'from it, or add *mappable* to an Axes.' ))
1259
- ax = self .gca ()
1260
- current_ax = self .gca ()
1261
- userax = False
1259
+ ax = current_ax
1262
1260
if (use_gridspec
1263
1261
and isinstance (ax , mpl .axes ._base ._AxesBase )
1264
1262
and ax .get_subplotspec ()):
1265
1263
cax , kwargs = cbar .make_axes_gridspec (ax , ** kwargs )
1266
1264
else :
1267
1265
cax , kwargs = cbar .make_axes (ax , ** kwargs )
1266
+ # make_axes calls add_{axes,subplot} which changes gca; undo that.
1267
+ self .sca (current_ax )
1268
1268
cax .grid (visible = False , which = 'both' , axis = 'both' )
1269
- else :
1270
- userax = True
1271
1269
1272
- # need to remove kws that cannot be passed to Colorbar
1273
- NON_COLORBAR_KEYS = ['fraction' , 'pad' , 'shrink' , 'aspect' , 'anchor' ,
1274
- 'panchor' ]
1275
- cb_kw = {k : v for k , v in kwargs .items () if k not in NON_COLORBAR_KEYS }
1276
-
1277
- cb = cbar .Colorbar (cax , mappable , ** cb_kw )
1278
-
1279
- if not userax :
1280
- self .sca (current_ax )
1270
+ NON_COLORBAR_KEYS = [ # remove kws that cannot be passed to Colorbar
1271
+ 'fraction' , 'pad' , 'shrink' , 'aspect' , 'anchor' , 'panchor' ]
1272
+ cb = cbar .Colorbar (cax , mappable , ** {
1273
+ k : v for k , v in kwargs .items () if k not in NON_COLORBAR_KEYS })
1281
1274
self .stale = True
1282
1275
return cb
1283
1276
0 commit comments