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 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
Next Next commit
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().
  • Loading branch information
efiring committed Aug 17, 2012
commit 329085ed74e0ea8f8e374c01d896db1842b065e9
26 changes: 25 additions & 1 deletion 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 = None, # whether to apply tight_layout
):
"""
*figsize*
Expand All @@ -276,6 +277,11 @@ def __init__(self,

*subplotpars*
A :class:`SubplotParams` instance, defaults to rc

*tight*
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(tight)

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

def _get_tight(self):
return self._tight
def _set_tight(self, tight):
if tight is None:
tight = rcParams['figure.autolayout']
tight = bool(tight)
self._tight = tight
tight = property(_get_tight, _set_tight,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I understand a foolish consistency is the hobgoblin of little minds, but shouldn't we stick with the normal setters and getters rather than introducing properties, so this will be exposed to the whole artist set/get introspection? It seems like it would add confusion to have some properties sets as plain-ol-attributes-or-properties, and some as setters/getters.

doc="If *True*, use :meth:`tight_layout`")

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 +880,13 @@ def draw(self, renderer):
if not self.get_visible(): return
renderer.open_group('figure')

if self.tight 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)
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