Skip to content

By default, don't change the figure face/edgecolor on savefig(). #15111

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 1 commit into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions doc/api/api_changes_3.3/behaviour.rst
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,10 @@ This also means there is a new keyword argument for `.axes.Axes.get_tightbbox`:
bounding box using the rules above. `.axis.Axis.get_tightbbox` gets an
``ignore_label`` keyword argument, which is *None* by default, but which can
also be 'x' or 'y'.

:rc:`savefig.facecolor` and :rc:`savefig.edgecolor` now default to "auto"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This newly allowed value for :rc:`savefig.facecolor` and :rc:`savefig.edgecolor`,
as well as the *facecolor* and *edgecolor* parameters to `.Figure.savefig`, means
"use whatever facecolor and edgecolor the figure current has".
20 changes: 12 additions & 8 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -2008,11 +2008,13 @@ def print_figure(
dpi : float, default: :rc:`savefig.dpi`
The dots per inch to save the figure in.

facecolor : color, default: :rc:`savefig.facecolor`
The facecolor of the figure.
facecolor : color or 'auto', default: :rc:`savefig.facecolor`
The facecolor of the figure. If 'auto', use the current figure
facecolor.

edgecolor : color, default: :rc:`savefig.edgecolor`
The edgecolor of the figure.
edgecolor : color or 'auto', default: :rc:`savefig.edgecolor`
The edgecolor of the figure. If 'auto', use the current figure
edgecolor.

orientation : {'landscape', 'portrait'}, default: 'portrait'
Only currently applies to PostScript printing.
Expand Down Expand Up @@ -2068,21 +2070,23 @@ def print_figure(
# but this should be fine.
with cbook._setattr_cm(self, _is_saving=True, manager=None), \
cbook._setattr_cm(self.figure, dpi=dpi):
origfacecolor = self.figure.get_facecolor()
origedgecolor = self.figure.get_edgecolor()

if facecolor is None:
facecolor = rcParams['savefig.facecolor']
if cbook._str_equal(facecolor, 'auto'):
facecolor = origfacecolor
if edgecolor is None:
edgecolor = rcParams['savefig.edgecolor']

origfacecolor = self.figure.get_facecolor()
origedgecolor = self.figure.get_edgecolor()
if cbook._str_equal(edgecolor, 'auto'):
edgecolor = origedgecolor

self.figure.set_facecolor(facecolor)
self.figure.set_edgecolor(edgecolor)

if bbox_inches is None:
bbox_inches = rcParams['savefig.bbox']

if bbox_inches:
if bbox_inches == "tight":
renderer = _get_renderer(
Expand Down
13 changes: 6 additions & 7 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -2097,11 +2097,13 @@ def savefig(self, fname, *, transparent=None, **kwargs):

This parameter is deprecated.

facecolor : color, default: :rc:`savefig.facecolor`
The facecolor of the figure.
facecolor : color or 'auto', default: :rc:`savefig.facecolor`
The facecolor of the figure. If 'auto', use the current figure
facecolor.

edgecolor : color, default: :rc:`savefig.edgecolor`
The edgecolor of the figure.
edgecolor : color or 'auto', default: :rc:`savefig.edgecolor`
The edgecolor of the figure. If 'auto', use the current figure
edgecolor.

orientation : {'landscape', 'portrait'}
Currently only supported by the postscript backend.
Expand Down Expand Up @@ -2172,9 +2174,6 @@ def savefig(self, fname, *, transparent=None, **kwargs):
patch.get_edgecolor()))
patch.set_facecolor('none')
patch.set_edgecolor('none')
else:
kwargs.setdefault('facecolor', mpl.rcParams['savefig.facecolor'])
kwargs.setdefault('edgecolor', mpl.rcParams['savefig.edgecolor'])

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

Expand Down
7 changes: 4 additions & 3 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1529,9 +1529,10 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
pil_kwargs["pnginfo"] = pnginfo
if format in ["jpg", "jpeg"]:
format = "jpeg" # Pillow doesn't recognize "jpg".
color = tuple(
int(x * 255)
for x in mcolors.to_rgb(mpl.rcParams["savefig.facecolor"]))
facecolor = mpl.rcParams["savefig.facecolor"]
if cbook._str_equal(facecolor, "auto"):
facecolor = mpl.rcParams["figure.facecolor"]
color = tuple(int(x * 255) for x in mcolors.to_rgb(facecolor))
background = PIL.Image.new("RGB", pil_shape, color)
background.paste(image, image)
image = background
Expand Down
8 changes: 4 additions & 4 deletions lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,13 @@ def validator(s):

def validate_color_or_inherit(s):
"""Return a valid color arg."""
if s == 'inherit':
if cbook._str_equal(s, 'inherit'):
return s
return validate_color(s)


def validate_color_or_auto(s):
if s == 'auto':
if cbook._str_equal(s, 'auto'):
return s
return validate_color(s)

Expand Down Expand Up @@ -1411,8 +1411,8 @@ def _convert_validator_spec(key, conv):

## Saving figure's properties
'savefig.dpi': ['figure', validate_dpi], # DPI
'savefig.facecolor': ['white', validate_color],
'savefig.edgecolor': ['white', validate_color],
'savefig.facecolor': ['auto', validate_color_or_auto],
'savefig.edgecolor': ['auto', validate_color_or_auto],
'savefig.orientation': ['portrait', ['landscape', 'portrait']],
'savefig.jpeg_quality': [95, validate_int],
# value checked by backend at runtime
Expand Down
4 changes: 2 additions & 2 deletions matplotlibrc.template
Original file line number Diff line number Diff line change
Expand Up @@ -641,8 +641,8 @@
## e.g., you may want a higher resolution, or to make the figure
## background white
#savefig.dpi: figure # figure dots per inch or 'figure'
#savefig.facecolor: white # figure facecolor when saving
#savefig.edgecolor: white # figure edgecolor when saving
#savefig.facecolor: auto # figure facecolor when saving
#savefig.edgecolor: auto # figure edgecolor when saving
#savefig.format: png # {png, ps, pdf, svg}
#savefig.bbox: standard # {tight, standard}
# 'tight' is incompatible with pipe-based animation
Expand Down