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

Skip to content

Commit b6ebe16

Browse files
committed
Deprecate savefig.facecolor, savefig.edgecolor, and savefig.transparent.
They cause a number of complications for print_figure (which needs to temporarily switch the figure and axes face and edge colors) that are unnecessary (the user can directly set the color they wish (including transparent) on the figure patch instead; an rcParam is even available for that purpose).
1 parent c46525a commit b6ebe16

14 files changed

+80
-83
lines changed

doc/api/api_changes/2017-12-16-AL.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Deprecation of savefig-time modification of the figure patch face and edge colors
2+
`````````````````````````````````````````````````````````````````````````````````
3+
4+
The ``facecolor``, ``edgecolor``, and ``transparent`` keyword arguments to
5+
`~.Figure.savefig` and `~.FigureCanvasBase.print_figure`, and the associated
6+
``savefig.facecolor``, ``savefig.edgecolor``, and ``savefig.transparent``
7+
rcParams, have been deprecated. As a replacement for the first two
8+
options, directly set the facecolor and edgecolor of the figure patch
9+
(``figure.patch``), or set the ``figure.facecolor`` and ``figure.edgecolor``
10+
rcParams. As a replacement for ``transparent``, set both the figure
11+
patch color and the axes patch color to transparent, or likewise set the
12+
corresponding rcParams.

lib/matplotlib/__init__.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,13 @@ def gen_candidates():
844844
_obsolete_set = {'text.dvipnghack'}
845845

846846
# The following may use a value of None to suppress the warning.
847-
_deprecated_set = {'axes.hold'} # do NOT include in _all_deprecated
847+
# Do NOT include in _all_deprecated.
848+
_deprecated_set = {
849+
'axes.hold',
850+
'savefig.facecolor',
851+
'savefig.edgecolor',
852+
'savefig.transparent',
853+
}
848854

849855
_all_deprecated = set(itertools.chain(
850856
_deprecated_ignore_map, _deprecated_map, _obsolete_set))
@@ -973,9 +979,11 @@ def rc_params(fail_on_error=False):
973979
if not os.path.exists(fname):
974980
# this should never happen, default in mpl-data should always be found
975981
message = 'could not find rc file; returning defaults'
976-
ret = RcParams([(key, default) for key, (default, _) in
977-
six.iteritems(defaultParams)
978-
if key not in _all_deprecated])
982+
with warnings.catch_warnings():
983+
warnings.filterwarnings("ignore", category=mplDeprecation)
984+
ret = RcParams([(key, default) for key, (default, _) in
985+
six.iteritems(defaultParams)
986+
if key not in _all_deprecated])
979987
warnings.warn(message)
980988
return ret
981989

@@ -1112,9 +1120,11 @@ def rc_params_from_file(fname, fail_on_error=False, use_default_template=True):
11121120
return config_from_file
11131121

11141122
iter_params = six.iteritems(defaultParams)
1115-
config = RcParams([(key, default) for key, (default, _) in iter_params
1116-
if key not in _all_deprecated])
1117-
config.update(config_from_file)
1123+
with warnings.catch_warnings():
1124+
warnings.filterwarnings("ignore", category=mplDeprecation)
1125+
config = RcParams([(key, default) for key, (default, _) in iter_params
1126+
if key not in _all_deprecated])
1127+
config.update(config_from_file)
11181128

11191129
if config['datapath'] is None:
11201130
config['datapath'] = get_data_path()
@@ -1151,9 +1161,11 @@ def rc_params_from_file(fname, fail_on_error=False, use_default_template=True):
11511161

11521162
rcParamsOrig = rcParams.copy()
11531163

1154-
rcParamsDefault = RcParams([(key, default) for key, (default, converter) in
1155-
six.iteritems(defaultParams)
1156-
if key not in _all_deprecated])
1164+
with warnings.catch_warnings():
1165+
warnings.filterwarnings("ignore", category=mplDeprecation)
1166+
rcParamsDefault = RcParams([(key, default) for key, (default, converter) in
1167+
six.iteritems(defaultParams)
1168+
if key not in _all_deprecated])
11571169

11581170
rcParams['ps.usedistiller'] = checkdep_ps_distiller(
11591171
rcParams['ps.usedistiller'])
@@ -1249,7 +1261,9 @@ def rcdefaults():
12491261
the default style.
12501262
"""
12511263
rcParams.clear()
1252-
rcParams.update(rcParamsDefault)
1264+
with warnings.catch_warnings():
1265+
warnings.filterwarnings("ignore", category=mplDeprecation)
1266+
rcParams.update(rcParamsDefault)
12531267

12541268

12551269
def rc_file_defaults():

lib/matplotlib/backend_bases.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2125,12 +2125,6 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
21252125
dpi : scalar, optional
21262126
the dots per inch to save the figure in; if None, use savefig.dpi
21272127
2128-
facecolor : color spec or None, optional
2129-
the facecolor of the figure; if None, defaults to savefig.facecolor
2130-
2131-
edgecolor : color spec or None, optional
2132-
the edgecolor of the figure; if None, defaults to savefig.edgecolor
2133-
21342128
format : str, optional
21352129
when set, forcibly set the file format to save to
21362130
@@ -2172,8 +2166,18 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
21722166

21732167
if facecolor is None:
21742168
facecolor = rcParams['savefig.facecolor']
2169+
else:
2170+
cbook.warn_deprecated(
2171+
"2.2",
2172+
"The 'facecolor' kwarg is deprecated; set the figure's "
2173+
"facecolor ('figure.patch.facecolor') instead")
21752174
if edgecolor is None:
21762175
edgecolor = rcParams['savefig.edgecolor']
2176+
else:
2177+
cbook.warn_deprecated(
2178+
"2.2",
2179+
"The 'edgecolor' kwarg is deprecated; set the figure's "
2180+
"edgecolor ('figure.patch.edgecolor') instead")
21772181

21782182
origDPI = self.figure.dpi
21792183
origfacecolor = self.figure.get_facecolor()

lib/matplotlib/backends/backend_agg.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -568,12 +568,9 @@ def print_jpg(self, filename_or_obj, *args, **kwargs):
568568
buf, size = self.print_to_buffer()
569569
if kwargs.pop("dryrun", False):
570570
return
571-
# The image is "pasted" onto a white background image to safely
572-
# handle any transparency
571+
# Paste image onto a white background to handle transparency.
573572
image = Image.frombuffer('RGBA', size, buf, 'raw', 'RGBA', 0, 1)
574-
rgba = mcolors.to_rgba(rcParams['savefig.facecolor'])
575-
color = tuple([int(x * 255.0) for x in rgba[:3]])
576-
background = Image.new('RGB', size, color)
573+
background = Image.new('RGB', size, (255, 255, 255))
577574
background.paste(image, image)
578575
options = {k: kwargs[k]
579576
for k in ['quality', 'optimize', 'progressive', 'dpi']

lib/matplotlib/backends/web_backend/nbagg_uat.ipynb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,7 @@
399399
"\n",
400400
"### UAT 15 - Figure face colours\n",
401401
"\n",
402-
"The nbagg honours all colours appart from that of the figure.patch. The two plots below should produce a figure with a red background. There should be no yellow figure."
403-
]
402+
"The nbagg honours all colours appart from that of the figure.patch. The two plots below should produce a figure with a red background."
404403
},
405404
{
406405
"cell_type": "code",
@@ -411,8 +410,7 @@
411410
"outputs": [],
412411
"source": [
413412
"import matplotlib\n",
414-
"matplotlib.rcParams.update({'figure.facecolor': 'red',\n",
415-
" 'savefig.facecolor': 'yellow'})\n",
413+
"matplotlib.rcParams.update({'figure.facecolor': 'red'})\n",
416414
"plt.figure()\n",
417415
"plt.plot([3, 2, 1])\n",
418416
"\n",

lib/matplotlib/figure.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,12 +1811,6 @@ def savefig(self, fname, **kwargs):
18111811
the value ``savefig.dpi`` in the matplotlibrc file. If 'figure'
18121812
it will set the dpi to be the value of the figure.
18131813
1814-
facecolor : color spec or None, optional
1815-
the facecolor of the figure; if None, defaults to savefig.facecolor
1816-
1817-
edgecolor : color spec or None, optional
1818-
the edgecolor of the figure; if None, defaults to savefig.edgecolor
1819-
18201814
orientation : {'landscape', 'portrait'}
18211815
not supported on all backends; currently only on postscript output
18221816
@@ -1829,15 +1823,6 @@ def savefig(self, fname, **kwargs):
18291823
One of the file extensions supported by the active
18301824
backend. Most backends support png, pdf, ps, eps and svg.
18311825
1832-
transparent : bool
1833-
If *True*, the axes patches will all be transparent; the
1834-
figure patch will also be transparent unless facecolor
1835-
and/or edgecolor are specified via kwargs.
1836-
This is useful, for example, for displaying
1837-
a plot on top of a colored background on a web page. The
1838-
transparency of these patches will be restored to their
1839-
original values upon exit of this function.
1840-
18411826
frameon : bool
18421827
If *True*, the figure patch will be colored, if *False*, the
18431828
figure background will be transparent. If not provided, the
@@ -1859,6 +1844,24 @@ def savefig(self, fname, **kwargs):
18591844
"""
18601845
kwargs.setdefault('dpi', rcParams['savefig.dpi'])
18611846
frameon = kwargs.pop('frameon', rcParams['savefig.frameon'])
1847+
1848+
if "transparent" in kwargs:
1849+
cbook.warn_deprecated(
1850+
"2.2",
1851+
"The 'transparent' kwarg is deprecated; set the figure's and "
1852+
"axes' facecolor ('figure.patch.facecolor', 'axes.patch."
1853+
"facecolor') to fully transparent ('none') instead")
1854+
if "facecolor" in kwargs:
1855+
cbook.warn_deprecated(
1856+
"2.2",
1857+
"The 'facecolor' kwarg is deprecated; set the figure's "
1858+
"facecolor ('figure.patch.facecolor') instead")
1859+
if "edgecolor" in kwargs:
1860+
cbook.warn_deprecated(
1861+
"2.2",
1862+
"The 'edgecolor' kwarg is deprecated; set the figure's "
1863+
"edgecolor ('figure.patch.edgecolor') instead")
1864+
18621865
transparent = kwargs.pop('transparent',
18631866
rcParams['savefig.transparent'])
18641867

@@ -1872,9 +1875,6 @@ def savefig(self, fname, **kwargs):
18721875
patch.get_edgecolor()))
18731876
patch.set_facecolor('none')
18741877
patch.set_edgecolor('none')
1875-
else:
1876-
kwargs.setdefault('facecolor', rcParams['savefig.facecolor'])
1877-
kwargs.setdefault('edgecolor', rcParams['savefig.edgecolor'])
18781878

18791879
if frameon:
18801880
original_frameon = self.get_frameon()

lib/matplotlib/mpl-data/stylelib/_classic_test.mplstyle

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,14 +294,13 @@ legend.facecolor : inherit # legend background color (when 'inherit' uses
294294
legend.edgecolor : inherit # legend edge color (when 'inherit' uses axes.edgecolor)
295295

296296

297-
298297
### FIGURE
299298
# See http://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure
300299
figure.titlesize : medium # size of the figure title
301300
figure.titleweight : normal # weight of the figure title
302301
figure.figsize : 8, 6 # figure size in inches
303302
figure.dpi : 80 # figure dots per inch
304-
figure.facecolor : 0.75 # figure facecolor; 0.75 is scalar gray
303+
figure.facecolor : w # figure facecolor
305304
figure.edgecolor : w # figure edgecolor
306305
figure.autolayout : False # When True, automatically adjust subplot
307306
# parameters to make the plot fit the figure
@@ -408,8 +407,6 @@ path.sketch : None # May be none, or a 3-tuple of the form (scale, length,
408407
# e.g., you may want a higher resolution, or to make the figure
409408
# background white
410409
savefig.dpi : 100 # figure dots per inch
411-
savefig.facecolor : w # figure facecolor when saving
412-
savefig.edgecolor : w # figure edgecolor when saving
413410
savefig.format : png # png, ps, pdf, svg
414411
savefig.bbox : standard # 'tight' or 'standard'.
415412
# 'tight' is incompatible with pipe-based animation
@@ -418,8 +415,6 @@ savefig.bbox : standard # 'tight' or 'standard'.
418415
# use ffmpeg_file instead
419416
savefig.pad_inches : 0.1 # Padding to be used when bbox is set to 'tight'
420417
savefig.jpeg_quality: 95 # when a jpeg is saved, the default quality parameter.
421-
savefig.transparent : False # setting that controls whether figures are saved with a
422-
# transparent background by default
423418
savefig.frameon : True
424419
savefig.orientation : portrait
425420

lib/matplotlib/mpl-data/stylelib/classic.mplstyle

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,6 @@ path.sketch : None # May be none, or a 3-tuple of the form (scale, length,
410410
# e.g., you may want a higher resolution, or to make the figure
411411
# background white
412412
savefig.dpi : 100 # figure dots per inch
413-
savefig.facecolor : w # figure facecolor when saving
414-
savefig.edgecolor : w # figure edgecolor when saving
415413
savefig.format : png # png, ps, pdf, svg
416414
savefig.bbox : standard # 'tight' or 'standard'.
417415
# 'tight' is incompatible with pipe-based animation
@@ -420,8 +418,6 @@ savefig.bbox : standard # 'tight' or 'standard'.
420418
# use ffmpeg_file instead
421419
savefig.pad_inches : 0.1 # Padding to be used when bbox is set to 'tight'
422420
savefig.jpeg_quality: 95 # when a jpeg is saved, the default quality parameter.
423-
savefig.transparent : False # setting that controls whether figures are saved with a
424-
# transparent background by default
425421
savefig.frameon : True
426422
savefig.orientation : portrait
427423

lib/matplotlib/mpl-data/stylelib/dark_background.mplstyle

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,3 @@ grid.color: white
1717

1818
figure.facecolor: black
1919
figure.edgecolor: black
20-
21-
savefig.facecolor: black
22-
savefig.edgecolor: black
23-

lib/matplotlib/mpl-data/stylelib/fivethirtyeight.mplstyle

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ ytick.minor.size: 0
3131

3232
font.size:14.0
3333

34-
savefig.edgecolor: f0f0f0
35-
savefig.facecolor: f0f0f0
36-
3734
figure.subplot.left: 0.08
3835
figure.subplot.right: 0.95
3936
figure.subplot.bottom: 0.07

lib/matplotlib/mpl-data/stylelib/grayscale.mplstyle

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,3 @@ figure.facecolor: 0.75
2323
figure.edgecolor: white
2424

2525
image.cmap: gray
26-
27-
savefig.facecolor: white
28-
savefig.edgecolor: white
29-

lib/matplotlib/tests/test_image.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -503,20 +503,16 @@ def test_jpeg_alpha():
503503

504504
plt.figimage(im)
505505

506-
buff = io.BytesIO()
507-
with rc_context({'savefig.facecolor': 'red'}):
508-
plt.savefig(buff, transparent=True, format='jpg', dpi=300)
506+
buf = io.BytesIO()
507+
plt.savefig(buf, transparent=True, format='jpg', dpi=300)
508+
buf.seek(0)
509+
image = Image.open(buf)
509510

510-
buff.seek(0)
511-
image = Image.open(buff)
512-
513-
# If this fails, there will be only one color (all black). If this
514-
# is working, we should have all 256 shades of grey represented.
515-
num_colors = len(image.getcolors(256))
516-
assert 175 <= num_colors <= 185
517-
# The fully transparent part should be red.
518-
corner_pixel = image.getpixel((0, 0))
519-
assert corner_pixel == (254, 0, 0)
511+
# If this fails, there will be only one color (all black). If this is
512+
# working, we should have all 256 shades of grey represented.
513+
assert len(image.getcolors(256)) == 256
514+
# The fully transparent part should be white.
515+
assert image.getpixel((0, 0)) == (255, 255, 255)
520516

521517

522518
def test_nonuniformimage_setdata():

matplotlibrc.template

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,6 @@ backend : $TEMPLATE_BACKEND
507507
# e.g., you may want a higher resolution, or to make the figure
508508
# background white
509509
#savefig.dpi : figure # figure dots per inch or 'figure'
510-
#savefig.facecolor : white # figure facecolor when saving
511-
#savefig.edgecolor : white # figure edgecolor when saving
512510
#savefig.format : png # png, ps, pdf, svg
513511
#savefig.bbox : standard # 'tight' or 'standard'.
514512
# 'tight' is incompatible with pipe-based animation
@@ -519,8 +517,6 @@ backend : $TEMPLATE_BACKEND
519517
#savefig.jpeg_quality: 95 # when a jpeg is saved, the default quality parameter.
520518
#savefig.directory : ~ # default directory in savefig dialog box,
521519
# leave empty to always use current working directory
522-
#savefig.transparent : False # setting that controls whether figures are saved with a
523-
# transparent background by default
524520

525521
# tk backend params
526522
#tk.window_focus : False # Maintain shell focus for TkAgg

tutorials/intermediate/tight_layout_guide.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import matplotlib.pyplot as plt
2525
import numpy as np
2626

27-
plt.rcParams['savefig.facecolor'] = "0.8"
27+
plt.rcParams['figure.facecolor'] = "0.8"
2828

2929

3030
def example_plot(ax, fontsize=12):

0 commit comments

Comments
 (0)