Permalink
Browse files

figure: improved auto tight_layout support

1) Use getter and setter instead of property
2) in tight_layout, don't proceed if an Axes is not a SubplotBase
3) in colorbar, make use_gridspec default to True.
  • Loading branch information...
1 parent 329085e commit ea0630a2369c12cd7b22cc00d966c05dae686a64 @efiring efiring committed Jul 22, 2012
Showing with 23 additions and 9 deletions.
  1. +23 −9 lib/matplotlib/figure.py
View
@@ -254,7 +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 = None, # whether to apply tight_layout
+ tight_layout = None, # default to rc figure.autolayout
):
"""
*figsize*
@@ -278,7 +278,7 @@ def __init__(self,
*subplotpars*
A :class:`SubplotParams` instance, defaults to rc
- *tight*
+ *tight_layout*
If *False* use *subplotpars*; if *True* adjust subplot
parameters using :meth:`tight_layout`. Defaults to
rc ``figure.autolayout``.
@@ -317,7 +317,7 @@ def __init__(self,
subplotpars = SubplotParams()
self.subplotpars = subplotpars
- self._set_tight(tight)
+ self.set_tight_layout(tight_layout)
self._axstack = AxesStack() # track all figure axes and current axes
self.clf()
@@ -336,15 +336,23 @@ def _set_dpi(self, dpi):
self.callbacks.process('dpi_changed', self)
dpi = property(_get_dpi, _set_dpi)
- def _get_tight(self):
+ def get_tight_layout(self):
+ """
+ Return the Boolean flag, True to use :meth`tight_layout` when drawing.
+ """
return self._tight
- def _set_tight(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
- tight = property(_get_tight, _set_tight,
- doc="If *True*, use :meth:`tight_layout`")
def autofmt_xdate(self, bottom=0.2, rotation=30, ha='right'):
"""
@@ -880,7 +888,7 @@ def draw(self, renderer):
if not self.get_visible(): return
renderer.open_group('figure')
- if self.tight and self.axes:
+ if self.get_tight_layout() and self.axes:
try:
self.tight_layout(renderer)
except ValueError:
@@ -1262,7 +1270,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)
@@ -1405,6 +1413,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)

0 comments on commit ea0630a

Please sign in to comment.