From 57462b6477e727d96be76310f19d203440ba3e35 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Fri, 23 Aug 2019 14:41:59 +0200 Subject: [PATCH] By default, don't change the figure face/edgecolor on savefig(). This seems to repeatedly confuse users. --- doc/api/api_changes_3.3/behaviour.rst | 7 +++++++ lib/matplotlib/backend_bases.py | 20 ++++++++++++-------- lib/matplotlib/figure.py | 13 ++++++------- lib/matplotlib/image.py | 7 ++++--- lib/matplotlib/rcsetup.py | 8 ++++---- matplotlibrc.template | 4 ++-- 6 files changed, 35 insertions(+), 24 deletions(-) diff --git a/doc/api/api_changes_3.3/behaviour.rst b/doc/api/api_changes_3.3/behaviour.rst index 09723ffd56a2..e69b7eff99e4 100644 --- a/doc/api/api_changes_3.3/behaviour.rst +++ b/doc/api/api_changes_3.3/behaviour.rst @@ -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". diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 271a24acbe9b..6502eadb7d49 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -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. @@ -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( diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index ef50382545cd..05087abbaf6b 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -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. @@ -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) diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index 0593521a1808..5393fd6df666 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -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 diff --git a/lib/matplotlib/rcsetup.py b/lib/matplotlib/rcsetup.py index 162f1d4ac30e..83225e807a1a 100644 --- a/lib/matplotlib/rcsetup.py +++ b/lib/matplotlib/rcsetup.py @@ -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) @@ -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 diff --git a/matplotlibrc.template b/matplotlibrc.template index bd98fbc3347c..6b0fc311af25 100644 --- a/matplotlibrc.template +++ b/matplotlibrc.template @@ -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