@@ -240,7 +240,7 @@ class Figure(Artist):
240
240
For multiple figure images, the figure will make composite
241
241
images depending on the renderer option_image_nocomposite
242
242
function. If suppressComposite is True|False, this will
243
- override the renderer
243
+ override the renderer.
244
244
"""
245
245
246
246
def __str__ (self ):
@@ -254,6 +254,7 @@ def __init__(self,
254
254
linewidth = 0.0 , # the default linewidth of the frame
255
255
frameon = True , # whether or not to draw the figure frame
256
256
subplotpars = None , # default to rc
257
+ tight_layout = None , # default to rc figure.autolayout
257
258
):
258
259
"""
259
260
*figsize*
@@ -276,6 +277,11 @@ def __init__(self,
276
277
277
278
*subplotpars*
278
279
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``.
279
285
"""
280
286
Artist .__init__ (self )
281
287
@@ -311,6 +317,7 @@ def __init__(self,
311
317
subplotpars = SubplotParams ()
312
318
313
319
self .subplotpars = subplotpars
320
+ self .set_tight_layout (tight_layout )
314
321
315
322
self ._axstack = AxesStack () # track all figure axes and current axes
316
323
self .clf ()
@@ -329,6 +336,24 @@ def _set_dpi(self, dpi):
329
336
self .callbacks .process ('dpi_changed' , self )
330
337
dpi = property (_get_dpi , _set_dpi )
331
338
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
+
332
357
def autofmt_xdate (self , bottom = 0.2 , rotation = 30 , ha = 'right' ):
333
358
"""
334
359
Date ticklabels often overlap, so it is useful to rotate them
@@ -865,6 +890,13 @@ def draw(self, renderer):
865
890
if not self .get_visible (): return
866
891
renderer .open_group ('figure' )
867
892
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
+
868
900
if self .frameon : self .patch .draw (renderer )
869
901
870
902
# a list of (zorder, func_to_call, list_of_args)
@@ -1244,7 +1276,7 @@ def colorbar(self, mappable, cax=None, ax=None, **kw):
1244
1276
"""
1245
1277
if ax is None :
1246
1278
ax = self .gca ()
1247
- use_gridspec = kw .pop ("use_gridspec" , False )
1279
+ use_gridspec = kw .pop ("use_gridspec" , True )
1248
1280
if cax is None :
1249
1281
if use_gridspec and isinstance (ax , SubplotBase ):
1250
1282
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
1387
1419
1388
1420
from tight_layout import get_renderer , get_tight_layout_figure
1389
1421
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
+
1390
1428
if renderer is None :
1391
1429
renderer = get_renderer (self )
1392
1430
0 commit comments