@@ -209,7 +209,33 @@ def get_renderer(fig):
209
209
return renderer
210
210
211
211
212
- def get_tight_layout_figure (fig , axes_list , renderer ,
212
+ def get_subplotspec_list (axes_list ):
213
+ """
214
+ Return a list of subplotspec from the given list of axes. For an
215
+ instance of axes that does not support subplotspec, None is
216
+ inserted in the list.
217
+
218
+ """
219
+ subplotspec_list = []
220
+ for ax in axes_list :
221
+ axes_or_locator = ax .get_axes_locator ()
222
+ if axes_or_locator is None :
223
+ axes_or_locator = ax
224
+
225
+ if hasattr (axes_or_locator , "get_subplotspec" ):
226
+ subplotspec = axes_or_locator .get_subplotspec ()
227
+ subplotspec = subplotspec .get_topmost_subplotspec ()
228
+ if subplotspec .get_gridspec ().locally_modified_subplot_params ():
229
+ subplotspec = None
230
+ else :
231
+ subplotspec = None
232
+
233
+ subplotspec_list .append (subplotspec )
234
+
235
+ return subplotspec_list
236
+
237
+
238
+ def get_tight_layout_figure (fig , axes_list , subplotspec_list , renderer ,
213
239
pad = 1.08 , h_pad = None , w_pad = None , rect = None ):
214
240
"""
215
241
Return subplot parameters for tight-layouted-figure with specified
@@ -221,6 +247,9 @@ def get_tight_layout_figure(fig, axes_list, renderer,
221
247
222
248
*axes_list* : a list of axes
223
249
250
+ *subplotspec_list* : a list of subplotspec associated with each
251
+ axes in axes_list
252
+
224
253
*renderer* : renderer instance
225
254
226
255
*pad* : float
@@ -238,27 +267,20 @@ def get_tight_layout_figure(fig, axes_list, renderer,
238
267
"""
239
268
240
269
241
- subplotspec_list = []
242
270
subplot_list = []
243
271
nrows_list = []
244
272
ncols_list = []
245
273
ax_bbox_list = []
246
274
247
- subplot_dict = {} # for axes_grid1, multiple axes can share
248
- # same subplot_interface. Thus we need to
249
- # join them together.
275
+ subplot_dict = {} # multiple axes can share
276
+ # same subplot_interface (e.g, axes_grid1). Thus
277
+ # we need to join them together.
250
278
251
- for ax in axes_list :
252
- locator = ax .get_axes_locator ()
253
- if hasattr (locator , "get_subplotspec" ):
254
- subplotspec = locator .get_subplotspec ().get_topmost_subplotspec ()
255
- elif hasattr (ax , "get_subplotspec" ):
256
- subplotspec = ax .get_subplotspec ().get_topmost_subplotspec ()
257
- else :
258
- continue
279
+ subplotspec_list2 = []
259
280
260
- if (subplotspec is None ) or \
261
- subplotspec .get_gridspec ().locally_modified_subplot_params ():
281
+ for ax , subplotspec in zip (axes_list ,
282
+ subplotspec_list ):
283
+ if subplotspec is None :
262
284
continue
263
285
264
286
subplots = subplot_dict .setdefault (subplotspec , [])
@@ -267,7 +289,7 @@ def get_tight_layout_figure(fig, axes_list, renderer,
267
289
myrows , mycols , _ , _ = subplotspec .get_geometry ()
268
290
nrows_list .append (myrows )
269
291
ncols_list .append (mycols )
270
- subplotspec_list .append (subplotspec )
292
+ subplotspec_list2 .append (subplotspec )
271
293
subplot_list .append (subplots )
272
294
ax_bbox_list .append (subplotspec .get_position (fig ))
273
295
@@ -277,7 +299,7 @@ def get_tight_layout_figure(fig, axes_list, renderer,
277
299
max_ncols = max (ncols_list )
278
300
279
301
num1num2_list = []
280
- for subplotspec in subplotspec_list :
302
+ for subplotspec in subplotspec_list2 :
281
303
rows , cols , num1 , num2 = subplotspec .get_geometry ()
282
304
div_row , mod_row = divmod (max_nrows , rows )
283
305
div_col , mod_col = divmod (max_ncols , cols )
0 commit comments