Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1552f80

Browse filesBrowse files
committed
By default, don't change the figure face/edgecolor on savefig().
This seems to repeatedly confuse users.
1 parent cc55b47 commit 1552f80
Copy full SHA for 1552f80

File tree

7 files changed

+55
-53
lines changed
Filter options

7 files changed

+55
-53
lines changed

‎doc/api/next_api_changes/behaviour.rst

Copy file name to clipboardExpand all lines: doc/api/next_api_changes/behaviour.rst
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,19 @@ shape ``(n, 2)`` would plot the first column of *x* against the first column
7070
of *y*, the second column of *x* against the second column of *y*, **and** the
7171
first column of *x* against the third column of *y*. This now raises an error
7272
instead.
73+
74+
:rc:`savefig.facecolor` and :rc:`savefig.edgecolor` now default to "auto"
75+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
76+
77+
This newly allowed value for :rc:`savefig.facecolor` and :rc:`savefig.edgecolor`,
78+
as well as the *facecolor* and *edgecolor* parameters to `.Figure.savefig`, means
79+
"use whatever facecolor and edgecolor the figure current has".
80+
81+
`.FigureCanvasPS.print_ps` and `.FigureCanvasPS.print_eps` no longer set the figure *facecolor* and *edgecolor*
82+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
83+
84+
Modification of the figure facecolor and edgecolor are already handled
85+
by `.FigureCanvasBase.print_figure` (which is in charge of calling
86+
`.FigureCanvasPS.print_ps` or `.FigureCanvasPS.print_eps`). This
87+
behavior is consistent with the other backend printing methods
88+
(`.FigureCanvasPdf.print_pdf`, `.FigureCanvasSVG.print_svg`, etc.)

‎lib/matplotlib/backend_bases.py

Copy file name to clipboardExpand all lines: lib/matplotlib/backend_bases.py
+23-21Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
The base class for the messaging area.
2929
"""
3030

31-
from contextlib import contextmanager
31+
from contextlib import contextmanager, ExitStack
3232
from enum import IntEnum
3333
import functools
3434
import importlib
@@ -1983,11 +1983,13 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
19831983
dpi : float, default: :rc:`savefig.dpi`
19841984
The dots per inch to save the figure in.
19851985
1986-
facecolor : color, default: :rc:`savefig.facecolor`
1987-
The facecolor of the figure.
1986+
facecolor : color or 'auto', default: :rc:`savefig.facecolor`
1987+
The facecolor of the figure. If 'auto', use the current figure
1988+
facecolor.
19881989
1989-
edgecolor : color, default: :rc:`savefig.edgecolor`
1990-
The edgecolor of the figure.
1990+
edgecolor : color or 'auto', default: :rc:`savefig.edgecolor`
1991+
The edgecolor of the figure. If 'auto', use the current figure
1992+
edgecolor.
19911993
19921994
format : str, optional
19931995
Force a specific file format. If not given, the format is inferred
@@ -2036,27 +2038,29 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
20362038
if dpi == 'figure':
20372039
dpi = getattr(self.figure, '_original_dpi', self.figure.dpi)
20382040

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

20572062
if bbox_inches is None:
20582063
bbox_inches = rcParams['savefig.bbox']
2059-
20602064
if bbox_inches:
20612065
if bbox_inches == "tight":
20622066
renderer = _get_renderer(
@@ -2094,8 +2098,6 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
20942098
if bbox_inches and restore_bbox:
20952099
restore_bbox()
20962100

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

‎lib/matplotlib/backends/backend_ps.py

Copy file name to clipboardExpand all lines: lib/matplotlib/backends/backend_ps.py
-16Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -849,11 +849,6 @@ def _print_figure(
849849
bbox = (llx, lly, urx, ury)
850850

851851
# generate PostScript code for the figure and store it in a string
852-
origfacecolor = self.figure.get_facecolor()
853-
origedgecolor = self.figure.get_edgecolor()
854-
self.figure.set_facecolor(facecolor)
855-
self.figure.set_edgecolor(edgecolor)
856-
857852
if dryrun:
858853
class NullWriter:
859854
def write(self, *args, **kwargs):
@@ -874,9 +869,6 @@ def write(self, *args, **kwargs):
874869
if dryrun: # return immediately if dryrun (tightbbox=True)
875870
return
876871

877-
self.figure.set_facecolor(origfacecolor)
878-
self.figure.set_edgecolor(origedgecolor)
879-
880872
# check for custom metadata
881873
if metadata is not None and 'Creator' in metadata:
882874
creator_str = metadata['Creator']
@@ -1040,11 +1032,6 @@ def _print_figure_tex(
10401032
bbox = (llx, lly, urx, ury)
10411033

10421034
# generate PostScript code for the figure and store it in a string
1043-
origfacecolor = self.figure.get_facecolor()
1044-
origedgecolor = self.figure.get_edgecolor()
1045-
self.figure.set_facecolor(facecolor)
1046-
self.figure.set_edgecolor(edgecolor)
1047-
10481035
if dryrun:
10491036
class NullWriter:
10501037
def write(self, *args, **kwargs):
@@ -1065,9 +1052,6 @@ def write(self, *args, **kwargs):
10651052
if dryrun: # return immediately if dryrun (tightbbox=True)
10661053
return
10671054

1068-
self.figure.set_facecolor(origfacecolor)
1069-
self.figure.set_edgecolor(origedgecolor)
1070-
10711055
# check for custom metadata
10721056
if metadata is not None and 'Creator' in metadata:
10731057
creator_str = metadata['Creator']

‎lib/matplotlib/figure.py

Copy file name to clipboardExpand all lines: lib/matplotlib/figure.py
+6-7Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2085,11 +2085,13 @@ def savefig(self, fname, *, transparent=None, **kwargs):
20852085
20862086
Whether the image should be stored as a progressive JPEG file.
20872087
2088-
facecolor : color, default: :rc:`savefig.facecolor`
2089-
The facecolor of the figure.
2088+
facecolor : color or 'auto', default: :rc:`savefig.facecolor`
2089+
The facecolor of the figure. If 'auto', use the current figure
2090+
facecolor.
20902091
2091-
edgecolor : color, default: :rc:`savefig.edgecolor`
2092-
The edgecolor of the figure.
2092+
edgecolor : color or 'auto', default: :rc:`savefig.edgecolor`
2093+
The edgecolor of the figure. If 'auto', use the current figure
2094+
edgecolor.
20932095
20942096
orientation : {'landscape', 'portrait'}
20952097
Currently only supported by the postscript backend.
@@ -2162,9 +2164,6 @@ def savefig(self, fname, *, transparent=None, **kwargs):
21622164
patch.get_edgecolor()))
21632165
patch.set_facecolor('none')
21642166
patch.set_edgecolor('none')
2165-
else:
2166-
kwargs.setdefault('facecolor', rcParams['savefig.facecolor'])
2167-
kwargs.setdefault('edgecolor', rcParams['savefig.edgecolor'])
21682167

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

‎lib/matplotlib/image.py

Copy file name to clipboardExpand all lines: lib/matplotlib/image.py
+4-3Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,9 +1545,10 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
15451545
pil_kwargs["pnginfo"] = pnginfo
15461546
if format in ["jpg", "jpeg"]:
15471547
format = "jpeg" # Pillow doesn't recognize "jpg".
1548-
color = tuple(
1549-
int(x * 255)
1550-
for x in mcolors.to_rgb(rcParams["savefig.facecolor"]))
1548+
facecolor = rcParams["savefig.facecolor"]
1549+
if cbook._str_equal(facecolor, "auto"):
1550+
facecolor = rcParams["figure.facecolor"]
1551+
color = tuple(int(x * 255) for x in mcolors.to_rgb(facecolor))
15511552
background = PIL.Image.new("RGB", pil_shape, color)
15521553
background.paste(image, image)
15531554
image = background

‎lib/matplotlib/rcsetup.py

Copy file name to clipboardExpand all lines: lib/matplotlib/rcsetup.py
+4-4Lines 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

@@ -1362,8 +1362,8 @@ def validate_webagg_address(s):
13621362

13631363
## Saving figure's properties
13641364
'savefig.dpi': ['figure', validate_dpi], # DPI
1365-
'savefig.facecolor': ['white', validate_color],
1366-
'savefig.edgecolor': ['white', validate_color],
1365+
'savefig.facecolor': ['auto', validate_color_or_auto],
1366+
'savefig.edgecolor': ['auto', validate_color_or_auto],
13671367
'savefig.orientation': ['portrait', validate_orientation],
13681368
'savefig.jpeg_quality': [95, validate_int],
13691369
# value checked by backend at runtime

‎matplotlibrc.template

Copy file name to clipboardExpand all lines: matplotlibrc.template
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,8 +647,8 @@
647647
## e.g., you may want a higher resolution, or to make the figure
648648
## background white
649649
#savefig.dpi : figure ## figure dots per inch or 'figure'
650-
#savefig.facecolor : white ## figure facecolor when saving
651-
#savefig.edgecolor : white ## figure edgecolor when saving
650+
#savefig.facecolor : auto ## figure facecolor when saving
651+
#savefig.edgecolor : auto ## figure edgecolor when saving
652652
#savefig.format : png ## {png, ps, pdf, svg}
653653
#savefig.bbox : standard ## {tight, standard}
654654
## 'tight' is incompatible with pipe-based animation

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.