Skip to content

Commit

Permalink
Merge pull request matplotlib#1004 from oxling/bbox_merge
Browse files Browse the repository at this point in the history
Added savefig.bbox option to matplotlibrc
  • Loading branch information
WeatherGod committed Jul 23, 2012
2 parents 63e406b + e6651c3 commit 561d0da
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
8 changes: 8 additions & 0 deletions doc/users/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ minimum and maximum colorbar extensions.

plt.show()


Set default bounding box in matplotlibrc
------------------------------------------

Two new defaults are available in the matplotlibrc configuration file.
These are savefig.bbox, which can be set to 'standard' or 'tight,' and
savefig.pad_inches, which controls the bounding box padding.

.. _whats-new-1-1:

new in matplotlib-1.1
Expand Down
13 changes: 10 additions & 3 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ def draw_quad_mesh(self, gc, master_transform, meshWidth, meshHeight,
:meth:`draw_quad_mesh` that generates paths and then calls
:meth:`draw_path_collection`.
"""

from matplotlib.collections import QuadMesh
paths = QuadMesh.convert_mesh_to_paths(
meshWidth, meshHeight, coordinates)
Expand Down Expand Up @@ -1977,11 +1978,11 @@ def print_figure(self, filename, dpi=None, facecolor='w', edgecolor='w',
*bbox_inches*
Bbox in inches. Only the given portion of the figure is
saved. If 'tight', try to figure out the tight bbox of
the figure.
the figure. If None, use savefig.bbox
*pad_inches*
Amount of padding around the figure when bbox_inches is
'tight'.
'tight'. If None, use savefig.pad_inches
*bbox_extra_artists*
A list of extra artists that will be considered when the
Expand All @@ -2003,6 +2004,7 @@ def print_figure(self, filename, dpi=None, facecolor='w', edgecolor='w',
if dpi is None:
dpi = rcParams['savefig.dpi']


origDPI = self.figure.dpi
origfacecolor = self.figure.get_facecolor()
origedgecolor = self.figure.get_edgecolor()
Expand All @@ -2012,6 +2014,9 @@ def print_figure(self, filename, dpi=None, facecolor='w', edgecolor='w',
self.figure.set_edgecolor(edgecolor)

bbox_inches = kwargs.pop("bbox_inches", None)
if bbox_inches is None:
bbox_inches = rcParams['savefig.bbox']


if bbox_inches:
# call adjust_bbox to save only the given area
Expand Down Expand Up @@ -2052,8 +2057,10 @@ def print_figure(self, filename, dpi=None, facecolor='w', edgecolor='w',

bbox_inches = Bbox.union([bbox_inches, bbox_inches1])

pad = kwargs.pop("pad_inches", None)
if pad is None:
pad = rcParams['savefig.pad_inches']

pad = kwargs.pop("pad_inches", 0.1)
bbox_inches = bbox_inches.padded(pad)

restore_bbox = tight_bbox.adjust_bbox(self.figure, format,
Expand Down
12 changes: 12 additions & 0 deletions lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,16 @@ def validate_hinting(s):
validate_movie_frame_fmt = ValidateInStrings('animation.frame_format',
['png', 'jpeg', 'tiff', 'raw', 'rgba'])

def validate_bbox(s):
if type(s) is str:
s = s.lower()
if s == 'tight':
return s
if s == 'standard':
return None
raise ValueError("bbox should be 'tight' or 'standard'")



class ValidateInterval:
"""
Expand Down Expand Up @@ -549,6 +559,8 @@ def __call__(self, s):
'savefig.orientation' : ['portrait', validate_orientation], # edgecolor; white
'savefig.extension' : ['png', deprecate_savefig_extension], # what to add to extensionless filenames
'savefig.format' : ['png', str], # value checked by backend at runtime
'savefig.bbox' : [None, validate_bbox], # options are 'tight', or 'standard'. 'standard' validates to None.
'savefig.pad_inches' : [0.1, validate_float],

'tk.window_focus' : [False, validate_bool], # Maintain shell focus for TkAgg
'tk.pythoninspect' : [False, validate_tkpythoninspect], # obsolete
Expand Down
10 changes: 6 additions & 4 deletions matplotlibrc.template
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,12 @@ text.hinting_factor : 8 # Specifies the amount of softness for hinting in the
# the default savefig params can be different from the display params
# Eg, you may want a higher resolution, or to make the figure
# background white
#savefig.dpi : 100 # figure dots per inch
#savefig.facecolor : white # figure facecolor when saving
#savefig.edgecolor : white # figure edgecolor when saving
#savefig.format : png # png, ps, pdf, svg
#savefig.dpi : 100 # figure dots per inch
#savefig.facecolor : white # figure facecolor when saving
#savefig.edgecolor : white # figure edgecolor when saving
#savefig.format : png # png, ps, pdf, svg
#savefig.bbox : standard # 'tight' or 'standard'.
#savefig.pad_inches : 0.1 # Padding to be used when bbox is set to 'tight'

# tk backend params
#tk.window_focus : False # Maintain shell focus for TkAgg
Expand Down

0 comments on commit 561d0da

Please sign in to comment.