@@ -263,6 +263,73 @@ def get_subplot_params(self, fig=None):
263263
264264 return subplotpars
265265
266+ def locally_modified_subplot_params (self ):
267+ return [k for k in self ._AllowedKeys if getattr (self , k )]
268+
269+
270+ def tight_layout (self , fig , renderer = None , pad = 1.2 , h_pad = None , w_pad = None , rect = None ):
271+ """Adjust subplot parameters to give specified padding.
272+
273+ Parameters
274+ ----------
275+ pad : float
276+ padding between the figure edge and the edges of subplots, as a fraction of the font-size.
277+ h_pad, w_pad : float
278+ padding (height/width) between edges of adjacent subplots.
279+ Defaults to `pad_inches`.
280+ """
281+
282+ from tight_layout import auto_adjust_subplotpars , get_renderer
283+
284+ if renderer is None :
285+ renderer = get_renderer (fig )
286+
287+ subplot_list = []
288+ num1num2_list = []
289+
290+ for ax in fig .axes :
291+ if hasattr (ax , "get_subplotspec" ):
292+ subplotspec = ax .get_subplotspec ()
293+ if subplotspec .get_gridspec () == self :
294+ subplot_list .append (ax )
295+ _ , _ , num1 , num2 = subplotspec .get_geometry ()
296+ num1num2_list .append ((num1 , num2 ))
297+
298+
299+ kwargs = auto_adjust_subplotpars (fig , renderer ,
300+ nrows_ncols = self .get_geometry (),
301+ num1num2_list = num1num2_list ,
302+ subplot_list = subplot_list ,
303+ pad = pad , h_pad = h_pad , w_pad = w_pad )
304+
305+ if rect is not None :
306+ # if rect is given, the whole subplots area (including
307+ # labels) will fit into the rect instead of the
308+ # figure. Note that the rect argument of
309+ # *auto_adjust_subplotpars* specify the area that will be
310+ # covered by the total area of axes.bbox. Thus we call
311+ # auto_adjust_subplotpars twice, where the second run
312+ # with adjusted rect parameters.
313+
314+ left , bottom , right , top = rect
315+ if left is not None : left += kwargs ["left" ]
316+ if bottom is not None : bottom += kwargs ["bottom" ]
317+ if right is not None : right -= (1 - kwargs ["right" ])
318+ if top is not None : top -= (1 - kwargs ["top" ])
319+
320+ #if h_pad is None: h_pad = pad
321+ #if w_pad is None: w_pad = pad
322+
323+ kwargs = auto_adjust_subplotpars (fig , renderer ,
324+ nrows_ncols = self .get_geometry (),
325+ num1num2_list = num1num2_list ,
326+ subplot_list = subplot_list ,
327+ pad = pad , h_pad = h_pad , w_pad = w_pad ,
328+ rect = (left , bottom , right , top ))
329+
330+
331+ self .update (** kwargs )
332+
266333
267334class GridSpecFromSubplotSpec (GridSpecBase ):
268335 """
0 commit comments