Skip to content
Browse files

Merge pull request #774 from efiring/figure_tight_kwarg

Allow automatic use of tight_layout.
  • Loading branch information...
2 parents b70c75d + ea0630a commit 62fe17fd51bf537b1212bb8e6632e2238e872b99 @pelson pelson committed Aug 21, 2012
Showing with 43 additions and 3 deletions.
  1. +40 −2 lib/matplotlib/figure.py
  2. +1 −1 lib/matplotlib/rcsetup.py
  3. +2 −0 matplotlibrc.template
View
42 lib/matplotlib/figure.py
@@ -240,7 +240,7 @@ class Figure(Artist):
For multiple figure images, the figure will make composite
images depending on the renderer option_image_nocomposite
function. If suppressComposite is True|False, this will
- override the renderer
+ override the renderer.
"""
def __str__(self):
@@ -254,6 +254,7 @@ def __init__(self,
linewidth = 0.0, # the default linewidth of the frame
frameon = True, # whether or not to draw the figure frame
subplotpars = None, # default to rc
+ tight_layout = None, # default to rc figure.autolayout
):
"""
*figsize*
@@ -276,6 +277,11 @@ def __init__(self,
*subplotpars*
A :class:`SubplotParams` instance, defaults to rc
+
+ *tight_layout*
+ If *False* use *subplotpars*; if *True* adjust subplot
+ parameters using :meth:`tight_layout`. Defaults to
+ rc ``figure.autolayout``.
"""
Artist.__init__(self)
@@ -311,6 +317,7 @@ def __init__(self,
subplotpars = SubplotParams()
self.subplotpars = subplotpars
+ self.set_tight_layout(tight_layout)
self._axstack = AxesStack() # track all figure axes and current axes
self.clf()
@@ -329,6 +336,24 @@ def _set_dpi(self, dpi):
self.callbacks.process('dpi_changed', self)
dpi = property(_get_dpi, _set_dpi)
+ def get_tight_layout(self):
+ """
+ Return the Boolean flag, True to use :meth`tight_layout` when drawing.
+ """
+ return self._tight
+
+ def set_tight_layout(self, tight):
+ """
+ Set whether :meth:`tight_layout` is used upon drawing.
+ If None, the rcParams['figure.autolayout'] value will be set.
+
+ ACCEPTS: [True | False | None]
+ """
+ if tight is None:
+ tight = rcParams['figure.autolayout']
+ tight = bool(tight)
+ self._tight = tight
+
def autofmt_xdate(self, bottom=0.2, rotation=30, ha='right'):
"""
Date ticklabels often overlap, so it is useful to rotate them
@@ -865,6 +890,13 @@ def draw(self, renderer):
if not self.get_visible(): return
renderer.open_group('figure')
+ if self.get_tight_layout() and self.axes:
+ try:
+ self.tight_layout(renderer)
+ except ValueError:
+ pass
+ # ValueError can occur when resizing a window.
+
if self.frameon: self.patch.draw(renderer)
# a list of (zorder, func_to_call, list_of_args)
@@ -1244,7 +1276,7 @@ def colorbar(self, mappable, cax=None, ax=None, **kw):
"""
if ax is None:
ax = self.gca()
- use_gridspec = kw.pop("use_gridspec", False)
+ use_gridspec = kw.pop("use_gridspec", True)
if cax is None:
if use_gridspec and isinstance(ax, SubplotBase):
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
from tight_layout import get_renderer, get_tight_layout_figure
+ no_go = [ax for ax in self.axes if not isinstance(ax, SubplotBase)]
+ if no_go:
+ warnings.Warn("Cannot use tight_layout;"
+ " all Axes must descend from SubplotBase")
+ return
+
if renderer is None:
renderer = get_renderer(self)
View
2 lib/matplotlib/rcsetup.py
@@ -547,7 +547,7 @@ def __call__(self, s):
'figure.dpi' : [ 80, validate_float], # DPI
'figure.facecolor' : [ '0.75', validate_color], # facecolor; scalar gray
'figure.edgecolor' : [ 'w', validate_color], # edgecolor; white
- 'figure.autolayout' : [ False, validate_autolayout],
+ 'figure.autolayout' : [ False, validate_bool],
'figure.subplot.left' : [0.125, ValidateInterval(0, 1, closedmin=True, closedmax=True)],
'figure.subplot.right' : [0.9, ValidateInterval(0, 1, closedmin=True, closedmax=True)],
View
2 matplotlibrc.template
@@ -301,6 +301,8 @@ text.hinting_factor : 8 # Specifies the amount of softness for hinting in the
#figure.dpi : 80 # figure dots per inch
#figure.facecolor : 0.75 # figure facecolor; 0.75 is scalar gray
#figure.edgecolor : white # figure edgecolor
+#figure.autolayout : False # When True, automatically adjust subplot
+ # parameters to make the plot fit the figure
# The figure subplot parameters. All dimensions are a fraction of the
# figure width or height

0 comments on commit 62fe17f

Please sign in to comment.
Something went wrong with that request. Please try again.