Skip to content

Commit 329085e

Browse files
committed
Allow automatic use of tight_layout.
In common interactive usage, stretching a figure across the screen leaves too much wasted space; Figure.tight_layout() can solve this problem at least for simple plots. This changeset uses a previously existing but unused rcParam and a Figure kwarg to allow the user to have tight_layout executed automatically with every Figure.draw().
1 parent d0c501b commit 329085e

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

lib/matplotlib/figure.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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 = None, # whether to apply tight_layout
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*
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(tight)
314321

315322
self._axstack = AxesStack() # track all figure axes and current axes
316323
self.clf()
@@ -329,6 +336,16 @@ def _set_dpi(self, dpi):
329336
self.callbacks.process('dpi_changed', self)
330337
dpi = property(_get_dpi, _set_dpi)
331338

339+
def _get_tight(self):
340+
return self._tight
341+
def _set_tight(self, tight):
342+
if tight is None:
343+
tight = rcParams['figure.autolayout']
344+
tight = bool(tight)
345+
self._tight = tight
346+
tight = property(_get_tight, _set_tight,
347+
doc="If *True*, use :meth:`tight_layout`")
348+
332349
def autofmt_xdate(self, bottom=0.2, rotation=30, ha='right'):
333350
"""
334351
Date ticklabels often overlap, so it is useful to rotate them
@@ -863,6 +880,13 @@ def draw(self, renderer):
863880
if not self.get_visible(): return
864881
renderer.open_group('figure')
865882

883+
if self.tight and self.axes:
884+
try:
885+
self.tight_layout(renderer)
886+
except ValueError:
887+
pass
888+
# ValueError can occur when resizing a window.
889+
866890
if self.frameon: self.patch.draw(renderer)
867891

868892
# a list of (zorder, func_to_call, list_of_args)

lib/matplotlib/rcsetup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ def __call__(self, s):
544544
'figure.dpi' : [ 80, validate_float], # DPI
545545
'figure.facecolor' : [ '0.75', validate_color], # facecolor; scalar gray
546546
'figure.edgecolor' : [ 'w', validate_color], # edgecolor; white
547-
'figure.autolayout' : [ False, validate_autolayout],
547+
'figure.autolayout' : [ False, validate_bool],
548548

549549
'figure.subplot.left' : [0.125, ValidateInterval(0, 1, closedmin=True, closedmax=True)],
550550
'figure.subplot.right' : [0.9, ValidateInterval(0, 1, closedmin=True, closedmax=True)],

matplotlibrc.template

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ text.hinting_factor : 8 # Specifies the amount of softness for hinting in the
301301
#figure.dpi : 80 # figure dots per inch
302302
#figure.facecolor : 0.75 # figure facecolor; 0.75 is scalar gray
303303
#figure.edgecolor : white # figure edgecolor
304+
#figure.autolayout : False # When True, automatically adjust subplot
305+
# parameters to make the plot fit the figure
304306

305307
# The figure subplot parameters. All dimensions are a fraction of the
306308
# figure width or height

0 commit comments

Comments
 (0)