@@ -277,41 +277,22 @@ def get_tight_layout_figure(fig, axes_list, subplotspec_list, renderer,
277
277
None if tight_layout could not be accomplished.
278
278
"""
279
279
280
- subplot_list = []
281
- nrows_list = []
282
- ncols_list = []
283
- ax_bbox_list = []
284
-
285
- # Multiple axes can share same subplot_interface (e.g., axes_grid1); thus
286
- # we need to join them together.
287
- subplot_dict = {}
288
-
289
- subplotspec_list2 = []
290
-
291
- for ax , subplotspec in zip (axes_list , subplotspec_list ):
292
- if subplotspec is None :
293
- continue
294
-
295
- subplots = subplot_dict .setdefault (subplotspec , [])
296
-
297
- if not subplots :
298
- myrows , mycols , _ , _ = subplotspec .get_geometry ()
299
- nrows_list .append (myrows )
300
- ncols_list .append (mycols )
301
- subplotspec_list2 .append (subplotspec )
302
- subplot_list .append (subplots )
303
- ax_bbox_list .append (subplotspec .get_position (fig ))
304
-
305
- subplots .append (ax )
306
-
307
- if len (nrows_list ) == 0 or len (ncols_list ) == 0 :
280
+ # Multiple axes can share same subplotspec (e.g., if using axes_grid1);
281
+ # we need to group them together.
282
+ ss_to_subplots = {ss : [] for ss in subplotspec_list }
283
+ for ax , ss in zip (axes_list , subplotspec_list ):
284
+ ss_to_subplots [ss ].append (ax )
285
+ ss_to_subplots .pop (None , None ) # Skip subplotspec == None.
286
+ if not ss_to_subplots :
308
287
return {}
288
+ subplot_list = list (ss_to_subplots .values ())
289
+ ax_bbox_list = [ss .get_position (fig ) for ss in ss_to_subplots ]
309
290
310
- max_nrows = max (nrows_list )
311
- max_ncols = max (ncols_list )
291
+ max_nrows = max (ss . get_gridspec (). nrows for ss in ss_to_subplots )
292
+ max_ncols = max (ss . get_gridspec (). ncols for ss in ss_to_subplots )
312
293
313
294
span_pairs = []
314
- for ss in subplotspec_list2 :
295
+ for ss in ss_to_subplots :
315
296
rows , cols = ss .get_gridspec ().get_geometry ()
316
297
div_row , mod_row = divmod (max_nrows , rows )
317
298
div_col , mod_col = divmod (max_ncols , cols )
@@ -325,7 +306,6 @@ def get_tight_layout_figure(fig, axes_list, subplotspec_list, renderer,
325
306
'columns in subplot specifications must be '
326
307
'multiples of one another.' )
327
308
return {}
328
-
329
309
span_pairs .append ((
330
310
slice (ss .rowspan .start * div_row , ss .rowspan .stop * div_row ),
331
311
slice (ss .colspan .start * div_col , ss .colspan .stop * div_col )))
0 commit comments