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

Skip to content

Commit 12a81c7

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

File tree

7 files changed

+63
-64
lines changed

7 files changed

+63
-64
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
:rc:`savefig.facecolor` and :rc:`savefig.edgecolor` now default to "auto"
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
This newly allowed value for :rc:`savefig.facecolor` and :rc:`savefig.edgecolor`,
5+
as well as the *facecolor* and *edgecolor* parameters to `.Figure.savefig`, means
6+
"use whatever facecolor and edgecolor the figure current has".
7+
8+
`.FigureCanvasPS.print_ps` and `.FigureCanvasPS.print_eps` no longer set the figure *facecolor* and *edgecolor*
9+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10+
11+
Modification of the figure facecolor and edgecolor are already handled
12+
by `.FigureCanvasBase.print_figure` (which is in charge of calling
13+
`.FigureCanvasPS.print_ps` or `.FigureCanvasPS.print_eps`). This
14+
behavior is consistent with the other backend printing methods
15+
(`.FigureCanvasPdf.print_pdf`, `.FigureCanvasSVG.print_svg`, etc.)

lib/matplotlib/backend_bases.py

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
The base class for the messaging area.
3333
"""
3434

35-
from contextlib import contextmanager
35+
from contextlib import contextmanager, ExitStack
3636
from enum import IntEnum
3737
import functools
3838
import importlib
@@ -1986,32 +1986,34 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
19861986
can also be a file object on image backends
19871987
19881988
orientation : {'landscape', 'portrait'}, optional
1989-
only currently applies to PostScript printing.
1989+
Only currently applies to PostScript printing.
19901990
19911991
dpi : float, default: :rc:`savefig.dpi`
19921992
The dots per inch to save the figure in.
19931993
1994-
facecolor : color or None, optional
1995-
the facecolor of the figure; if None, defaults to savefig.facecolor
1994+
facecolor : color or 'auto' or None, optional
1995+
The facecolor of the figure. If 'auto', use the current figure
1996+
facecolor. If None, use :rc:`savefig.facecolor`.
19961997
1997-
edgecolor : color or None, optional
1998-
the edgecolor of the figure; if None, defaults to savefig.edgecolor
1998+
edgecolor : color or 'auto' or None, optional
1999+
The edgecolor of the figure. If 'auto', use the current figure
2000+
edgecolor. If None, use :rc:`savefig.edgecolor`.
19992001
20002002
format : str, optional
2001-
when set, forcibly set the file format to save to
2003+
When set, forcibly set the file format to save to.
20022004
20032005
bbox_inches : str or `~matplotlib.transforms.Bbox`, optional
2004-
Bbox in inches. Only the given portion of the figure is
2005-
saved. If 'tight', try to figure out the tight bbox of
2006-
the figure. If None, use savefig.bbox
2006+
Bbox in inches. Only the given portion of the figure is saved. If
2007+
'tight', try to figure out the tight bbox of the figure. If None,
2008+
use :rc:`savefig.bbox`.
20072009
20082010
pad_inches : scalar, optional
2009-
Amount of padding around the figure when bbox_inches is
2010-
'tight'. If None, use savefig.pad_inches
2011+
Amount of padding around the figure when bbox_inches is 'tight'. If
2012+
None, use :rc:`savefig.pad_inches`.
20112013
20122014
bbox_extra_artists : list of `~matplotlib.artist.Artist`, optional
2013-
A list of extra artists that will be considered when the
2014-
tight bbox is calculated.
2015+
A list of extra artists that will be considered when the tight bbox
2016+
is calculated.
20152017
20162018
"""
20172019
if format is None:
@@ -2035,27 +2037,29 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
20352037
if dpi == 'figure':
20362038
dpi = getattr(self.figure, '_original_dpi', self.figure.dpi)
20372039

2038-
# Remove the figure manager, if any, to avoid resizing the GUI widget.
2039-
# Some code (e.g. Figure.show) differentiates between having *no*
2040-
# manager and a *None* manager, which should be fixed at some point,
2041-
# but this should be fine.
2042-
with cbook._setattr_cm(self, _is_saving=True, manager=None), \
2043-
cbook._setattr_cm(self.figure, dpi=dpi):
2044-
2040+
with ExitStack() as stack:
2041+
# Remove the figure manager, if any, to avoid resizing the GUI
2042+
# widget. Some code (e.g. Figure.show) differentiates between
2043+
# having *no* manager and a *None* manager, which should be fixed
2044+
# at some point, but this should be fine.
2045+
stack.enter_context(
2046+
cbook._setattr_cm(self, _is_saving=True, manager=None))
2047+
stack.enter_context(cbook._setattr_cm(self.figure, dpi=dpi))
20452048
if facecolor is None:
20462049
facecolor = rcParams['savefig.facecolor']
2050+
if not cbook._str_equal(facecolor, 'auto'):
2051+
stack.callback(
2052+
self.figure.set_facecolor, self.figure.get_facecolor())
2053+
self.figure.set_facecolor(facecolor)
20472054
if edgecolor is None:
20482055
edgecolor = rcParams['savefig.edgecolor']
2049-
2050-
origfacecolor = self.figure.get_facecolor()
2051-
origedgecolor = self.figure.get_edgecolor()
2052-
2053-
self.figure.set_facecolor(facecolor)
2054-
self.figure.set_edgecolor(edgecolor)
2056+
if not cbook._str_equal(edgecolor, 'auto'):
2057+
stack.callback(
2058+
self.figure.set_edgecolor, self.figure.get_edgecolor())
2059+
self.figure.set_edgecolor(edgecolor)
20552060

20562061
if bbox_inches is None:
20572062
bbox_inches = rcParams['savefig.bbox']
2058-
20592063
if bbox_inches:
20602064
if bbox_inches == "tight":
20612065
renderer = _get_renderer(
@@ -2093,8 +2097,6 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
20932097
if bbox_inches and restore_bbox:
20942098
restore_bbox()
20952099

2096-
self.figure.set_facecolor(origfacecolor)
2097-
self.figure.set_edgecolor(origedgecolor)
20982100
self.figure.set_canvas(self)
20992101
return result
21002102

lib/matplotlib/backends/backend_ps.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -916,11 +916,6 @@ def _print_figure(
916916
bbox = (llx, lly, urx, ury)
917917

918918
# generate PostScript code for the figure and store it in a string
919-
origfacecolor = self.figure.get_facecolor()
920-
origedgecolor = self.figure.get_edgecolor()
921-
self.figure.set_facecolor(facecolor)
922-
self.figure.set_edgecolor(edgecolor)
923-
924919
if dryrun:
925920
class NullWriter:
926921
def write(self, *args, **kwargs):
@@ -941,9 +936,6 @@ def write(self, *args, **kwargs):
941936
if dryrun: # return immediately if dryrun (tightbbox=True)
942937
return
943938

944-
self.figure.set_facecolor(origfacecolor)
945-
self.figure.set_edgecolor(origedgecolor)
946-
947939
# check for custom metadata
948940
if metadata is not None and 'Creator' in metadata:
949941
creator_str = metadata['Creator']
@@ -1113,11 +1105,6 @@ def _print_figure_tex(
11131105
bbox = (llx, lly, urx, ury)
11141106

11151107
# generate PostScript code for the figure and store it in a string
1116-
origfacecolor = self.figure.get_facecolor()
1117-
origedgecolor = self.figure.get_edgecolor()
1118-
self.figure.set_facecolor(facecolor)
1119-
self.figure.set_edgecolor(edgecolor)
1120-
11211108
if dryrun:
11221109
class NullWriter:
11231110
def write(self, *args, **kwargs):
@@ -1138,9 +1125,6 @@ def write(self, *args, **kwargs):
11381125
if dryrun: # return immediately if dryrun (tightbbox=True)
11391126
return
11401127

1141-
self.figure.set_facecolor(origfacecolor)
1142-
self.figure.set_edgecolor(origedgecolor)
1143-
11441128
# check for custom metadata
11451129
if metadata is not None and 'Creator' in metadata:
11461130
creator_str = metadata['Creator']

lib/matplotlib/figure.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,13 +2089,13 @@ def savefig(self, fname, *, transparent=None, **kwargs):
20892089
20902090
Whether the image should be stored as a progressive JPEG file.
20912091
2092-
facecolor : color or None, optional
2093-
The facecolor of the figure; if *None*, defaults to
2094-
:rc:`savefig.facecolor`.
2092+
facecolor : color or 'auto' or None, optional
2093+
The facecolor of the figure. If 'auto', use the current figure
2094+
facecolor. If None, use :rc:`savefig.facecolor`.
20952095
2096-
edgecolor : color or None, optional
2097-
The edgecolor of the figure; if *None*, defaults to
2098-
:rc:`savefig.edgecolor`
2096+
edgecolor : color or 'auto' or None, optional
2097+
The edgecolor of the figure. If 'auto', use the current figure
2098+
edgecolor. If None, use :rc:`savefig.edgecolor`.
20992099
21002100
orientation : {'landscape', 'portrait'}
21012101
Currently only supported by the postscript backend.
@@ -2170,9 +2170,6 @@ def savefig(self, fname, *, transparent=None, **kwargs):
21702170
patch.get_edgecolor()))
21712171
patch.set_facecolor('none')
21722172
patch.set_edgecolor('none')
2173-
else:
2174-
kwargs.setdefault('facecolor', rcParams['savefig.facecolor'])
2175-
kwargs.setdefault('edgecolor', rcParams['savefig.edgecolor'])
21762173

21772174
if frameon:
21782175
original_frameon = self.patch.get_visible()

lib/matplotlib/image.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,9 +1507,10 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
15071507
pil_kwargs["pnginfo"] = pnginfo
15081508
if format in ["jpg", "jpeg"]:
15091509
format = "jpeg" # Pillow doesn't recognize "jpg".
1510-
color = tuple(
1511-
int(x * 255)
1512-
for x in mcolors.to_rgb(rcParams["savefig.facecolor"]))
1510+
facecolor = rcParams["savefig.facecolor"]
1511+
if cbook._str_equal(facecolor, "auto"):
1512+
facecolor = rcParams["figure.facecolor"]
1513+
color = tuple(int(x * 255) for x in mcolors.to_rgb(facecolor))
15131514
background = PIL.Image.new("RGB", pil_shape, color)
15141515
background.paste(image, image)
15151516
image = background

lib/matplotlib/rcsetup.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,13 +297,13 @@ def validator(s):
297297

298298
def validate_color_or_inherit(s):
299299
"""Return a valid color arg."""
300-
if s == 'inherit':
300+
if cbook._str_equal(s, 'inherit'):
301301
return s
302302
return validate_color(s)
303303

304304

305305
def validate_color_or_auto(s):
306-
if s == 'auto':
306+
if cbook._str_equal(s, 'auto'):
307307
return s
308308
return validate_color(s)
309309

@@ -1371,8 +1371,8 @@ def _validate_linestyle(ls):
13711371

13721372
## Saving figure's properties
13731373
'savefig.dpi': ['figure', validate_dpi], # DPI
1374-
'savefig.facecolor': ['white', validate_color],
1375-
'savefig.edgecolor': ['white', validate_color],
1374+
'savefig.facecolor': ['auto', validate_color_or_auto],
1375+
'savefig.edgecolor': ['auto', validate_color_or_auto],
13761376
'savefig.frameon': [True, validate_bool],
13771377
'savefig.orientation': ['portrait', validate_orientation],
13781378
'savefig.jpeg_quality': [95, validate_int],

matplotlibrc.template

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

0 commit comments

Comments
 (0)