Thanks to visit codestin.com
Credit goes to github.com

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
Show file tree
Hide file tree
Changes from all commits
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
42 changes: 40 additions & 2 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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*
Expand All @@ -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)

Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -863,6 +888,13 @@ def draw(self, renderer):
if not self.get_visible(): return
renderer.open_group('figure')

if self.get_tight_layout() and self.axes:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly is being tested wrt self.axes? Being None? Being of length greater than zero. I know PEP8 encourages this sort of semantics, but it is a pain in the butt to me to understand the intention.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.axes is used all over the place in Figure; it is a read-only list of axes. It is being tested for emptiness. I could enclose it in len(), but it would not add anything.

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)
Expand Down Expand Up @@ -1238,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 @@ -1381,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
2 changes: 1 addition & 1 deletion lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,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)],
Expand Down
2 changes: 2 additions & 0 deletions matplotlibrc.template
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down