Skip to content

Allow automatic use of tight_layout. #774

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 21, 2012
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
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
efiring committed Aug 17, 2012
commit ea0630a2369c12cd7b22cc00d966c05dae686a64
32 changes: 23 additions & 9 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand All @@ -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``.
Expand Down Expand Up @@ -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()
Expand All @@ -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'):
"""
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down