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

Skip to content

Commit 4047815

Browse files
committed
Added savefig.bbox option to matplotlibrc
Allows the user to set the bounding box to 'tight,' which will override the defaults when saving from the interface.
1 parent 8c57e4f commit 4047815

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 3 additions & 1 deletion
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)
@@ -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()
@@ -2011,7 +2013,7 @@ def print_figure(self, filename, dpi=None, facecolor='w', edgecolor='w',
20112013
self.figure.set_facecolor(facecolor)
20122014
self.figure.set_edgecolor(edgecolor)
20132015

2014-
bbox_inches = kwargs.pop("bbox_inches", None)
2016+
bbox_inches = kwargs.pop("bbox_inches", rcParams['savefig.bbox'])
20152017

20162018
if bbox_inches:
20172019
# call adjust_bbox to save only the given area

lib/matplotlib/rcsetup.py

Lines changed: 11 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 in ('tight'):
332+
return s
333+
if s in ('auto'):
334+
return None
335+
raise ValueError("bbox_inches should be 'auto' or 'tight'")
336+
337+
328338

329339
class ValidateInterval:
330340
"""
@@ -549,6 +559,7 @@ 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 auto. Auto validates to 'None'
552563

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

matplotlibrc.template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ text.hinting_factor : 8 # Specifies the amount of softness for hinting in the
350350
#savefig.facecolor : white # figure facecolor when saving
351351
#savefig.edgecolor : white # figure edgecolor when saving
352352
#savefig.format : png # png, ps, pdf, svg
353+
#savefig.bbox : auto # tight, auto. When set to 'tight', will set the figure's bounding box to 'tight' and recalculate. When set to 'auto', it will use the automatic bounding box calculation.
353354

354355
# tk backend params
355356
#tk.window_focus : False # Maintain shell focus for TkAgg

0 commit comments

Comments
 (0)