@@ -1179,7 +1179,7 @@ def make_axes_gridspec(parents, **kw):
1179
1179
'''
1180
1180
Resize and reposition a parent axes, and return a child axes
1181
1181
suitable for a colorbar. This function is similar to
1182
- make_axes. Prmary differences are
1182
+ make_axes. Primary differences are
1183
1183
1184
1184
* *make_axes_gridspec* only handles the *orientation* keyword
1185
1185
and cannot handle the "location" keyword.
@@ -1224,10 +1224,11 @@ def make_axes_gridspec(parents, **kw):
1224
1224
1225
1225
# make parents a 1-d ndarray if its not already...
1226
1226
parents = np .atleast_1d (parents ).ravel ()
1227
- # get the appropriate subplot spec. Loop through the parents.
1227
+ # For the subplotspec that these axes belong to, loop through and get
1228
+ # the maximum and minimum index into the subplotspec. The result is the
1229
+ # size the new gridspec that we will create must be.
1228
1230
gsp0 = parents [0 ].get_subplotspec ().get_gridspec ()
1229
- minind = 10000
1230
- maxind = - 10000
1231
+ minind = 10000 ;maxind = - 10000
1231
1232
for parent in parents :
1232
1233
gsp = parent .get_subplotspec ().get_gridspec ()
1233
1234
if gsp == gsp0 :
@@ -1238,47 +1239,37 @@ def make_axes_gridspec(parents, **kw):
1238
1239
if ss [3 ]> maxind :
1239
1240
maxind = ss [3 ]
1240
1241
else :
1241
- pass
1242
- # get from maxind and minind to nrows and ncols..
1242
+ raise NotImplementedError ('List of axes passed to colorbar must be from the same gridspec or call to plt.subplots' )
1243
+ # map the extent of the gridspec from indices to rows and columns.
1244
+ # We need this below to assign the parents into the new gridspec.
1243
1245
ncols0 = gsp0 .get_geometry ()[1 ]
1244
- print (ncols0 )
1245
- print (maxind )
1246
-
1247
1246
minrow ,mincol = index2rowcolunm (minind ,ncols0 )
1248
1247
maxrow ,maxcol = index2rowcolunm (maxind ,ncols0 )
1249
- print ('mnind %d maxind %d' % (minind ,maxind ))
1250
- print ('mncol %d maxcol %d' % (mincol ,maxcol ))
1251
- print ('mnrow %d maxrow %d' % (minrow ,maxrow ))
1252
1248
nrows = maxrow - minrow + 1
1253
1249
ncols = maxcol - mincol + 1
1254
1250
1255
- print ('nrows %d ncols %d' % (nrows ,ncols ))
1256
- print (minind )
1257
- print (maxind )
1251
+ # this is subplot spec the region that we need to resize and add
1252
+ # a colorbar to.
1258
1253
subspec = gridspec .SubplotSpec (gsp0 ,minind ,maxind )
1259
- print ('GSP0' )
1260
- print (gsp0 .get_geometry ())
1261
- print (subspec )
1262
- print (subspec .get_geometry ())
1263
1254
1264
1255
gs_from_subplotspec = gridspec .GridSpecFromSubplotSpec
1265
1256
if orientation == 'vertical' :
1266
1257
pad = kw .pop ('pad' , 0.05 )
1267
1258
wh_space = 2 * pad / (1 - pad )
1268
1259
1260
+ # split the subplotspec containing parents into two, with one only
1261
+ # `fraction` wide for the colorbar, and the other with some padding.
1269
1262
gs = gs_from_subplotspec (1 , 2 ,
1270
1263
subplot_spec = subspec ,
1271
1264
wspace = wh_space ,
1272
1265
width_ratios = [x1 - pad , fraction ]
1273
1266
)
1274
-
1267
+ # center the colorbar vertically.
1275
1268
gs2 = gs_from_subplotspec (3 , 1 ,
1276
1269
subplot_spec = gs [1 ],
1277
1270
hspace = 0. ,
1278
1271
height_ratios = wh_ratios ,
1279
1272
)
1280
- print (gs )
1281
- print (gs2 )
1282
1273
1283
1274
anchor = (0.0 , 0.5 )
1284
1275
panchor = (1.0 , 0.5 )
@@ -1310,18 +1301,22 @@ def make_axes_gridspec(parents, **kw):
1310
1301
for parent in parents :
1311
1302
geo = parent .get_subplotspec ().get_geometry ()
1312
1303
ncol0 = geo [1 ]
1313
- print ("Parent Geo:" )
1314
- print (geo )
1304
+
1305
+ # remap the old min gridspec index (geo[2]) into a new
1306
+ # index.
1315
1307
oldrow ,oldcol = index2rowcolunm (geo [2 ],ncol0 )
1316
1308
newrow = oldrow - minrow + 1
1317
1309
newcol = oldcol - mincol + 1
1318
1310
newminind = rowcolunm2index (newrow ,newcol ,ncols )
1311
+
1312
+ # remap the old max gridspec index (geo[3]) into a new
1313
+ # index.
1319
1314
oldrow ,oldcol = index2rowcolunm (geo [3 ],ncol0 )
1320
1315
newrow = oldrow - minrow + 1
1321
1316
newcol = oldcol - mincol + 1
1322
1317
newmaxind = rowcolunm2index (newrow ,newcol ,ncols )
1323
- print ('newminind %d newmaxind %d' % (newminind ,newmaxind ))
1324
1318
1319
+ # change the subplotspec for this parent.
1325
1320
parent .set_subplotspec (gridspec .SubplotSpec (gsnew ,newminind ,newmaxind ))
1326
1321
parent .update_params ()
1327
1322
parent .set_position (parent .figbox )
0 commit comments