Skip to content

Commit 57462b6

Browse files
committed
By default, don't change the figure face/edgecolor on savefig().
This seems to repeatedly confuse users.
1 parent d541b6d commit 57462b6

File tree

6 files changed

+35
-24
lines changed

6 files changed

+35
-24
lines changed

doc/api/api_changes_3.3/behaviour.rst

+7
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,10 @@ This also means there is a new keyword argument for `.axes.Axes.get_tightbbox`:
234234
bounding box using the rules above. `.axis.Axis.get_tightbbox` gets an
235235
``ignore_label`` keyword argument, which is *None* by default, but which can
236236
also be 'x' or 'y'.
237+
238+
:rc:`savefig.facecolor` and :rc:`savefig.edgecolor` now default to "auto"
239+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
240+
241+
This newly allowed value for :rc:`savefig.facecolor` and :rc:`savefig.edgecolor`,
242+
as well as the *facecolor* and *edgecolor* parameters to `.Figure.savefig`, means
243+
"use whatever facecolor and edgecolor the figure current has".

lib/matplotlib/backend_bases.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -2008,11 +2008,13 @@ def print_figure(
20082008
dpi : float, default: :rc:`savefig.dpi`
20092009
The dots per inch to save the figure in.
20102010
2011-
facecolor : color, default: :rc:`savefig.facecolor`
2012-
The facecolor of the figure.
2011+
facecolor : color or 'auto', default: :rc:`savefig.facecolor`
2012+
The facecolor of the figure. If 'auto', use the current figure
2013+
facecolor.
20132014
2014-
edgecolor : color, default: :rc:`savefig.edgecolor`
2015-
The edgecolor of the figure.
2015+
edgecolor : color or 'auto', default: :rc:`savefig.edgecolor`
2016+
The edgecolor of the figure. If 'auto', use the current figure
2017+
edgecolor.
20162018
20172019
orientation : {'landscape', 'portrait'}, default: 'portrait'
20182020
Only currently applies to PostScript printing.
@@ -2068,21 +2070,23 @@ def print_figure(
20682070
# but this should be fine.
20692071
with cbook._setattr_cm(self, _is_saving=True, manager=None), \
20702072
cbook._setattr_cm(self.figure, dpi=dpi):
2073+
origfacecolor = self.figure.get_facecolor()
2074+
origedgecolor = self.figure.get_edgecolor()
20712075

20722076
if facecolor is None:
20732077
facecolor = rcParams['savefig.facecolor']
2078+
if cbook._str_equal(facecolor, 'auto'):
2079+
facecolor = origfacecolor
20742080
if edgecolor is None:
20752081
edgecolor = rcParams['savefig.edgecolor']
2076-
2077-
origfacecolor = self.figure.get_facecolor()
2078-
origedgecolor = self.figure.get_edgecolor()
2082+
if cbook._str_equal(edgecolor, 'auto'):
2083+
edgecolor = origedgecolor
20792084

20802085
self.figure.set_facecolor(facecolor)
20812086
self.figure.set_edgecolor(edgecolor)
20822087

20832088
if bbox_inches is None:
20842089
bbox_inches = rcParams['savefig.bbox']
2085-
20862090
if bbox_inches:
20872091
if bbox_inches == "tight":
20882092
renderer = _get_renderer(

lib/matplotlib/figure.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -2097,11 +2097,13 @@ def savefig(self, fname, *, transparent=None, **kwargs):
20972097
20982098
This parameter is deprecated.
20992099
2100-
facecolor : color, default: :rc:`savefig.facecolor`
2101-
The facecolor of the figure.
2100+
facecolor : color or 'auto', default: :rc:`savefig.facecolor`
2101+
The facecolor of the figure. If 'auto', use the current figure
2102+
facecolor.
21022103
2103-
edgecolor : color, default: :rc:`savefig.edgecolor`
2104-
The edgecolor of the figure.
2104+
edgecolor : color or 'auto', default: :rc:`savefig.edgecolor`
2105+
The edgecolor of the figure. If 'auto', use the current figure
2106+
edgecolor.
21052107
21062108
orientation : {'landscape', 'portrait'}
21072109
Currently only supported by the postscript backend.
@@ -2172,9 +2174,6 @@ def savefig(self, fname, *, transparent=None, **kwargs):
21722174
patch.get_edgecolor()))
21732175
patch.set_facecolor('none')
21742176
patch.set_edgecolor('none')
2175-
else:
2176-
kwargs.setdefault('facecolor', mpl.rcParams['savefig.facecolor'])
2177-
kwargs.setdefault('edgecolor', mpl.rcParams['savefig.edgecolor'])
21782177

21792178
self.canvas.print_figure(fname, **kwargs)
21802179

lib/matplotlib/image.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1529,9 +1529,10 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
15291529
pil_kwargs["pnginfo"] = pnginfo
15301530
if format in ["jpg", "jpeg"]:
15311531
format = "jpeg" # Pillow doesn't recognize "jpg".
1532-
color = tuple(
1533-
int(x * 255)
1534-
for x in mcolors.to_rgb(mpl.rcParams["savefig.facecolor"]))
1532+
facecolor = mpl.rcParams["savefig.facecolor"]
1533+
if cbook._str_equal(facecolor, "auto"):
1534+
facecolor = mpl.rcParams["figure.facecolor"]
1535+
color = tuple(int(x * 255) for x in mcolors.to_rgb(facecolor))
15351536
background = PIL.Image.new("RGB", pil_shape, color)
15361537
background.paste(image, image)
15371538
image = background

lib/matplotlib/rcsetup.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -324,13 +324,13 @@ def validator(s):
324324

325325
def validate_color_or_inherit(s):
326326
"""Return a valid color arg."""
327-
if s == 'inherit':
327+
if cbook._str_equal(s, 'inherit'):
328328
return s
329329
return validate_color(s)
330330

331331

332332
def validate_color_or_auto(s):
333-
if s == 'auto':
333+
if cbook._str_equal(s, 'auto'):
334334
return s
335335
return validate_color(s)
336336

@@ -1411,8 +1411,8 @@ def _convert_validator_spec(key, conv):
14111411

14121412
## Saving figure's properties
14131413
'savefig.dpi': ['figure', validate_dpi], # DPI
1414-
'savefig.facecolor': ['white', validate_color],
1415-
'savefig.edgecolor': ['white', validate_color],
1414+
'savefig.facecolor': ['auto', validate_color_or_auto],
1415+
'savefig.edgecolor': ['auto', validate_color_or_auto],
14161416
'savefig.orientation': ['portrait', ['landscape', 'portrait']],
14171417
'savefig.jpeg_quality': [95, validate_int],
14181418
# value checked by backend at runtime

matplotlibrc.template

+2-2
Original file line numberDiff line numberDiff line change
@@ -641,8 +641,8 @@
641641
## e.g., you may want a higher resolution, or to make the figure
642642
## background white
643643
#savefig.dpi: figure # figure dots per inch or 'figure'
644-
#savefig.facecolor: white # figure facecolor when saving
645-
#savefig.edgecolor: white # figure edgecolor when saving
644+
#savefig.facecolor: auto # figure facecolor when saving
645+
#savefig.edgecolor: auto # figure edgecolor when saving
646646
#savefig.format: png # {png, ps, pdf, svg}
647647
#savefig.bbox: standard # {tight, standard}
648648
# 'tight' is incompatible with pipe-based animation

0 commit comments

Comments
 (0)