@@ -207,3 +207,120 @@ def get_renderer(fig):
207207 renderer = canvas .get_renderer ()
208208
209209 return renderer
210+
211+
212+ def get_tight_layout_figure (fig , axes_list , renderer ,
213+ pad = 1.2 , h_pad = None , w_pad = None , rect = None ):
214+ """
215+ return subplot parameters for tigh-layouted- figure with
216+ specified padding.
217+
218+ Parameters:
219+
220+ *fig* : figure instance
221+ *axes_list* : a list of axes
222+ *renderer* : renderer instance
223+ *pad* : float
224+ padding between the figure edge and the edges of subplots,
225+ as a fraction of the font-size.
226+ *h_pad*, *w_pad* : float
227+ padding (height/width) between edges of adjacent subplots.
228+ Defaults to `pad_inches`.
229+ *rect* : if rect is given, it is interpreted as a rectangle
230+ (left, bottom, right, top) in the normalized figure
231+ coordinate that the whole subplots area (including
232+ labels) will fit into. Default is (0, 0, 1, 1).
233+ """
234+
235+
236+ subplotspec_list = []
237+ subplot_list = []
238+ nrows_list = []
239+ ncols_list = []
240+ ax_bbox_list = []
241+
242+ subplot_dict = {} # for axes_grid1, multiple axes can share
243+ # same subplot_interface. Thus we need to
244+ # join them together.
245+
246+ for ax in axes_list :
247+ locator = ax .get_axes_locator ()
248+ if hasattr (locator , "get_subplotspec" ):
249+ subplotspec = locator .get_subplotspec ().get_topmost_subplotspec ()
250+ elif hasattr (ax , "get_subplotspec" ):
251+ subplotspec = ax .get_subplotspec ().get_topmost_subplotspec ()
252+ else :
253+ continue
254+
255+ if (subplotspec is None ) or \
256+ subplotspec .get_gridspec ().locally_modified_subplot_params ():
257+ continue
258+
259+ subplots = subplot_dict .setdefault (subplotspec , [])
260+
261+ if not subplots :
262+ myrows , mycols , _ , _ = subplotspec .get_geometry ()
263+ nrows_list .append (myrows )
264+ ncols_list .append (mycols )
265+ subplotspec_list .append (subplotspec )
266+ subplot_list .append (subplots )
267+ ax_bbox_list .append (subplotspec .get_position (fig ))
268+
269+ subplots .append (ax )
270+
271+ max_nrows = max (nrows_list )
272+ max_ncols = max (ncols_list )
273+
274+ num1num2_list = []
275+ for subplotspec in subplotspec_list :
276+ rows , cols , num1 , num2 = subplotspec .get_geometry ()
277+ div_row , mod_row = divmod (max_nrows , rows )
278+ div_col , mod_col = divmod (max_ncols , cols )
279+ if (mod_row != 0 ) or (mod_col != 0 ):
280+ raise RuntimeError ("" )
281+
282+ rowNum1 , colNum1 = divmod (num1 , cols )
283+ if num2 is None :
284+ rowNum2 , colNum2 = rowNum1 , colNum1
285+ else :
286+ rowNum2 , colNum2 = divmod (num2 , cols )
287+
288+ num1num2_list .append ((rowNum1 * div_row * max_ncols + colNum1 * div_col ,
289+ ((rowNum2 + 1 )* div_row - 1 )* max_ncols + (colNum2 + 1 )* div_col - 1 ))
290+
291+
292+ kwargs = auto_adjust_subplotpars (fig , renderer ,
293+ nrows_ncols = (max_nrows , max_ncols ),
294+ num1num2_list = num1num2_list ,
295+ subplot_list = subplot_list ,
296+ ax_bbox_list = ax_bbox_list ,
297+ pad = pad , h_pad = h_pad , w_pad = w_pad )
298+
299+ if rect is not None :
300+ # if rect is given, the whole subplots area (including
301+ # labels) will fit into the rect instead of the
302+ # figure. Note that the rect argument of
303+ # *auto_adjust_subplotpars* specify the area that will be
304+ # covered by the total area of axes.bbox. Thus we call
305+ # auto_adjust_subplotpars twice, where the second run
306+ # with adjusted rect parameters.
307+
308+ left , bottom , right , top = rect
309+ if left is not None : left += kwargs ["left" ]
310+ if bottom is not None : bottom += kwargs ["bottom" ]
311+ if right is not None : right -= (1 - kwargs ["right" ])
312+ if top is not None : top -= (1 - kwargs ["top" ])
313+
314+ #if h_pad is None: h_pad = pad
315+ #if w_pad is None: w_pad = pad
316+
317+ kwargs = auto_adjust_subplotpars (fig , renderer ,
318+ nrows_ncols = (max_nrows , max_ncols ),
319+ num1num2_list = num1num2_list ,
320+ subplot_list = subplot_list ,
321+ ax_bbox_list = ax_bbox_list ,
322+ pad = pad , h_pad = h_pad , w_pad = w_pad ,
323+ rect = (left , bottom , right , top ))
324+
325+ return kwargs
326+
0 commit comments