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

Skip to content

Commit 561d0da

Browse files
committed
Merge pull request #1004 from oxling/bbox_merge
Added savefig.bbox option to matplotlibrc
2 parents 63e406b + e6651c3 commit 561d0da

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

doc/users/whats_new.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ minimum and maximum colorbar extensions.
5757

5858
plt.show()
5959

60+
61+
Set default bounding box in matplotlibrc
62+
------------------------------------------
63+
64+
Two new defaults are available in the matplotlibrc configuration file.
65+
These are savefig.bbox, which can be set to 'standard' or 'tight,' and
66+
savefig.pad_inches, which controls the bounding box padding.
67+
6068
.. _whats-new-1-1:
6169

6270
new in matplotlib-1.1

lib/matplotlib/backend_bases.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ def draw_quad_mesh(self, gc, master_transform, meshWidth, meshHeight,
230230
:meth:`draw_quad_mesh` that generates paths and then calls
231231
:meth:`draw_path_collection`.
232232
"""
233+
233234
from matplotlib.collections import QuadMesh
234235
paths = QuadMesh.convert_mesh_to_paths(
235236
meshWidth, meshHeight, coordinates)
@@ -1977,11 +1978,11 @@ def print_figure(self, filename, dpi=None, facecolor='w', edgecolor='w',
19771978
*bbox_inches*
19781979
Bbox in inches. Only the given portion of the figure is
19791980
saved. If 'tight', try to figure out the tight bbox of
1980-
the figure.
1981+
the figure. If None, use savefig.bbox
19811982
19821983
*pad_inches*
19831984
Amount of padding around the figure when bbox_inches is
1984-
'tight'.
1985+
'tight'. If None, use savefig.pad_inches
19851986
19861987
*bbox_extra_artists*
19871988
A list of extra artists that will be considered when the
@@ -2003,6 +2004,7 @@ def print_figure(self, filename, dpi=None, facecolor='w', edgecolor='w',
20032004
if dpi is None:
20042005
dpi = rcParams['savefig.dpi']
20052006

2007+
20062008
origDPI = self.figure.dpi
20072009
origfacecolor = self.figure.get_facecolor()
20082010
origedgecolor = self.figure.get_edgecolor()
@@ -2012,6 +2014,9 @@ def print_figure(self, filename, dpi=None, facecolor='w', edgecolor='w',
20122014
self.figure.set_edgecolor(edgecolor)
20132015

20142016
bbox_inches = kwargs.pop("bbox_inches", None)
2017+
if bbox_inches is None:
2018+
bbox_inches = rcParams['savefig.bbox']
2019+
20152020

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

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

2060+
pad = kwargs.pop("pad_inches", None)
2061+
if pad is None:
2062+
pad = rcParams['savefig.pad_inches']
20552063

2056-
pad = kwargs.pop("pad_inches", 0.1)
20572064
bbox_inches = bbox_inches.padded(pad)
20582065

20592066
restore_bbox = tight_bbox.adjust_bbox(self.figure, format,

lib/matplotlib/rcsetup.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,16 @@ def validate_hinting(s):
325325
validate_movie_frame_fmt = ValidateInStrings('animation.frame_format',
326326
['png', 'jpeg', 'tiff', 'raw', 'rgba'])
327327

328+
def validate_bbox(s):
329+
if type(s) is str:
330+
s = s.lower()
331+
if s == 'tight':
332+
return s
333+
if s == 'standard':
334+
return None
335+
raise ValueError("bbox should be 'tight' or 'standard'")
336+
337+
328338

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

553565
'tk.window_focus' : [False, validate_bool], # Maintain shell focus for TkAgg
554566
'tk.pythoninspect' : [False, validate_tkpythoninspect], # obsolete

matplotlibrc.template

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,10 +346,12 @@ text.hinting_factor : 8 # Specifies the amount of softness for hinting in the
346346
# the default savefig params can be different from the display params
347347
# Eg, you may want a higher resolution, or to make the figure
348348
# background white
349-
#savefig.dpi : 100 # figure dots per inch
350-
#savefig.facecolor : white # figure facecolor when saving
351-
#savefig.edgecolor : white # figure edgecolor when saving
352-
#savefig.format : png # png, ps, pdf, svg
349+
#savefig.dpi : 100 # figure dots per inch
350+
#savefig.facecolor : white # figure facecolor when saving
351+
#savefig.edgecolor : white # figure edgecolor when saving
352+
#savefig.format : png # png, ps, pdf, svg
353+
#savefig.bbox : standard # 'tight' or 'standard'.
354+
#savefig.pad_inches : 0.1 # Padding to be used when bbox is set to 'tight'
353355

354356
# tk backend params
355357
#tk.window_focus : False # Maintain shell focus for TkAgg

0 commit comments

Comments
 (0)