@@ -240,7 +240,7 @@ class Figure(Artist):
240240 For multiple figure images, the figure will make composite
241241 images depending on the renderer option_image_nocomposite
242242 function. If suppressComposite is True|False, this will
243- override the renderer
243+ override the renderer.
244244 """
245245
246246 def __str__ (self ):
@@ -254,6 +254,7 @@ def __init__(self,
254254 linewidth = 0.0 , # the default linewidth of the frame
255255 frameon = True , # whether or not to draw the figure frame
256256 subplotpars = None , # default to rc
257+ tight_layout = None , # default to rc figure.autolayout
257258 ):
258259 """
259260 *figsize*
@@ -276,6 +277,11 @@ def __init__(self,
276277
277278 *subplotpars*
278279 A :class:`SubplotParams` instance, defaults to rc
280+
281+ *tight_layout*
282+ If *False* use *subplotpars*; if *True* adjust subplot
283+ parameters using :meth:`tight_layout`. Defaults to
284+ rc ``figure.autolayout``.
279285 """
280286 Artist .__init__ (self )
281287
@@ -311,6 +317,7 @@ def __init__(self,
311317 subplotpars = SubplotParams ()
312318
313319 self .subplotpars = subplotpars
320+ self .set_tight_layout (tight_layout )
314321
315322 self ._axstack = AxesStack () # track all figure axes and current axes
316323 self .clf ()
@@ -329,6 +336,24 @@ def _set_dpi(self, dpi):
329336 self .callbacks .process ('dpi_changed' , self )
330337 dpi = property (_get_dpi , _set_dpi )
331338
339+ def get_tight_layout (self ):
340+ """
341+ Return the Boolean flag, True to use :meth`tight_layout` when drawing.
342+ """
343+ return self ._tight
344+
345+ def set_tight_layout (self , tight ):
346+ """
347+ Set whether :meth:`tight_layout` is used upon drawing.
348+ If None, the rcParams['figure.autolayout'] value will be set.
349+
350+ ACCEPTS: [True | False | None]
351+ """
352+ if tight is None :
353+ tight = rcParams ['figure.autolayout' ]
354+ tight = bool (tight )
355+ self ._tight = tight
356+
332357 def autofmt_xdate (self , bottom = 0.2 , rotation = 30 , ha = 'right' ):
333358 """
334359 Date ticklabels often overlap, so it is useful to rotate them
@@ -865,6 +890,13 @@ def draw(self, renderer):
865890 if not self .get_visible (): return
866891 renderer .open_group ('figure' )
867892
893+ if self .get_tight_layout () and self .axes :
894+ try :
895+ self .tight_layout (renderer )
896+ except ValueError :
897+ pass
898+ # ValueError can occur when resizing a window.
899+
868900 if self .frameon : self .patch .draw (renderer )
869901
870902 # a list of (zorder, func_to_call, list_of_args)
@@ -1244,7 +1276,7 @@ def colorbar(self, mappable, cax=None, ax=None, **kw):
12441276 """
12451277 if ax is None :
12461278 ax = self .gca ()
1247- use_gridspec = kw .pop ("use_gridspec" , False )
1279+ use_gridspec = kw .pop ("use_gridspec" , True )
12481280 if cax is None :
12491281 if use_gridspec and isinstance (ax , SubplotBase ):
12501282 cax , kw = cbar .make_axes_gridspec (ax , ** kw )
@@ -1387,6 +1419,12 @@ def tight_layout(self, renderer=None, pad=1.08, h_pad=None, w_pad=None, rect=Non
13871419
13881420 from tight_layout import get_renderer , get_tight_layout_figure
13891421
1422+ no_go = [ax for ax in self .axes if not isinstance (ax , SubplotBase )]
1423+ if no_go :
1424+ warnings .Warn ("Cannot use tight_layout;"
1425+ " all Axes must descend from SubplotBase" )
1426+ return
1427+
13901428 if renderer is None :
13911429 renderer = get_renderer (self )
13921430
0 commit comments