@@ -255,36 +255,41 @@ def check_no_collapsed_axes(layoutgrids, fig):
255
255
256
256
257
257
def compress_fixed_aspect (layoutgrids , fig ):
258
- extraw = dict ()
259
- extrah = dict ()
258
+ gs = None
260
259
for ax in fig .axes :
261
260
if hasattr (ax , 'get_subplotspec' ):
262
261
actual = ax .get_position (original = False )
263
262
ax .apply_aspect ()
264
263
sub = ax .get_subplotspec ()
265
- gs = sub .get_gridspec ()
266
- if gs not in extraw :
267
- extraw [gs ] = np .zeros (gs .ncols )
268
- extrah [gs ] = np .zeros (gs .nrows )
264
+ _gs = sub .get_gridspec ()
265
+ if gs is None :
266
+ gs = _gs
267
+ extraw = np .zeros (gs .ncols )
268
+ extrah = np .zeros (gs .nrows )
269
+ elif _gs != gs :
270
+ raise ValueError ('Cannot do compressed layout if axes are not' ,
271
+ 'all from the same gridspec' )
269
272
orig = ax .get_position (original = True )
270
273
actual = ax .get_position (original = False )
271
274
dw = orig .width - actual .width
272
275
if dw > 0 :
273
- extraw [gs ][sub .colspan ] = np .maximum (extraw [gs ][sub .colspan ],
274
- dw )
276
+ extraw [sub .colspan ] = np .maximum (extraw [sub .colspan ], dw )
275
277
dh = orig .height - actual .height
276
278
if dh > 0 :
277
- extrah [gs ][sub .rowspan ] = np .maximum (extrah [gs ][sub .rowspan ],
278
- dh )
279
+ extrah [sub .rowspan ] = np .maximum (extrah [sub .rowspan ], dh )
279
280
280
- w = np .sum (extraw [gs ]) / 2
281
- layoutgrids [fig ].edit_margin_min ('left' , w )
282
- layoutgrids [fig ].edit_margin_min ('right' , w )
281
+ if gs is not None :
282
+ w = np .sum (extraw ) / 2
283
+ layoutgrids [fig ].edit_margin_min ('left' , w )
284
+ layoutgrids [fig ].edit_margin_min ('right' , w )
283
285
284
- h = np .sum (extrah [gs ]) / 2
285
- layoutgrids [fig ].edit_margin_min ('top' , h )
286
- layoutgrids [fig ].edit_margin_min ('bottom' , h )
287
- return layoutgrids
286
+ h = np .sum (extrah ) / 2
287
+ layoutgrids [fig ].edit_margin_min ('top' , h )
288
+ layoutgrids [fig ].edit_margin_min ('bottom' , h )
289
+ return layoutgrids
290
+ else :
291
+ raise ValueError ('Cannot do compressed layout if no axes ' ,
292
+ 'are part of a gridspec.' )
288
293
289
294
290
295
def get_margin_from_padding (obj , * , w_pad = 0 , h_pad = 0 ,
0 commit comments