32
32
The base class for the messaging area.
33
33
"""
34
34
35
- from contextlib import contextmanager
35
+ from contextlib import contextmanager , ExitStack
36
36
from enum import IntEnum
37
37
import functools
38
38
import importlib
@@ -2001,32 +2001,35 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
2001
2001
can also be a file object on image backends
2002
2002
2003
2003
orientation : {'landscape', 'portrait'}, optional
2004
- only currently applies to PostScript printing.
2004
+ Only currently applies to PostScript printing.
2005
2005
2006
2006
dpi : scalar, optional
2007
- the dots per inch to save the figure in; if None, use savefig.dpi
2007
+ The dots per inch to save the figure in; if None, use
2008
+ :rc:`savefig.dpi`.
2008
2009
2009
- facecolor : color or None, optional
2010
- the facecolor of the figure; if None, defaults to savefig.facecolor
2010
+ facecolor : color or 'auto' or None, optional
2011
+ The facecolor of the figure. If 'auto', use the current figure
2012
+ facecolor. If None, use :rc:`savefig.facecolor`.
2011
2013
2012
- edgecolor : color or None, optional
2013
- the edgecolor of the figure; if None, defaults to savefig.edgecolor
2014
+ edgecolor : color or 'auto' or None, optional
2015
+ The edgecolor of the figure. If 'auto', use the current figure
2016
+ edgecolor. If None, use :rc:`savefig.edgecolor`.
2014
2017
2015
2018
format : str, optional
2016
- when set, forcibly set the file format to save to
2019
+ When set, forcibly set the file format to save to.
2017
2020
2018
2021
bbox_inches : str or `~matplotlib.transforms.Bbox`, optional
2019
- Bbox in inches. Only the given portion of the figure is
2020
- saved. If 'tight', try to figure out the tight bbox of
2021
- the figure. If None, use savefig.bbox
2022
+ Bbox in inches. Only the given portion of the figure is saved. If
2023
+ 'tight', try to figure out the tight bbox of the figure. If None,
2024
+ use :rc:` savefig.bbox`.
2022
2025
2023
2026
pad_inches : scalar, optional
2024
- Amount of padding around the figure when bbox_inches is
2025
- 'tight'. If None, use savefig.pad_inches
2027
+ Amount of padding around the figure when bbox_inches is 'tight'. If
2028
+ None, use :rc:` savefig.pad_inches`.
2026
2029
2027
2030
bbox_extra_artists : list of `~matplotlib.artist.Artist`, optional
2028
- A list of extra artists that will be considered when the
2029
- tight bbox is calculated.
2031
+ A list of extra artists that will be considered when the tight bbox
2032
+ is calculated.
2030
2033
2031
2034
"""
2032
2035
if format is None :
@@ -2050,27 +2053,29 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
2050
2053
if dpi == 'figure' :
2051
2054
dpi = getattr (self .figure , '_original_dpi' , self .figure .dpi )
2052
2055
2053
- # Remove the figure manager, if any, to avoid resizing the GUI widget.
2054
- # Some code (e.g. Figure.show) differentiates between having *no*
2055
- # manager and a *None* manager, which should be fixed at some point,
2056
- # but this should be fine.
2057
- with cbook ._setattr_cm (self , _is_saving = True , manager = None ), \
2058
- cbook ._setattr_cm (self .figure , dpi = dpi ):
2059
-
2056
+ with ExitStack () as stack :
2057
+ # Remove the figure manager, if any, to avoid resizing the GUI
2058
+ # widget. Some code (e.g. Figure.show) differentiates between
2059
+ # having *no* manager and a *None* manager, which should be fixed
2060
+ # at some point, but this should be fine.
2061
+ stack .enter_context (
2062
+ cbook ._setattr_cm (self , _is_saving = True , manager = None ))
2063
+ stack .enter_context (cbook ._setattr_cm (self .figure , dpi = dpi ))
2060
2064
if facecolor is None :
2061
2065
facecolor = rcParams ['savefig.facecolor' ]
2066
+ if not cbook ._str_equal (facecolor , 'auto' ):
2067
+ stack .callback (
2068
+ self .figure .set_facecolor , self .figure .get_facecolor ())
2069
+ self .figure .set_facecolor (facecolor )
2062
2070
if edgecolor is None :
2063
2071
edgecolor = rcParams ['savefig.edgecolor' ]
2064
-
2065
- origfacecolor = self .figure .get_facecolor ()
2066
- origedgecolor = self .figure .get_edgecolor ()
2067
-
2068
- self .figure .set_facecolor (facecolor )
2069
- self .figure .set_edgecolor (edgecolor )
2072
+ if not cbook ._str_equal (edgecolor , 'auto' ):
2073
+ stack .callback (
2074
+ self .figure .set_edgecolor , self .figure .get_edgecolor ())
2075
+ self .figure .set_edgecolor (edgecolor )
2070
2076
2071
2077
if bbox_inches is None :
2072
2078
bbox_inches = rcParams ['savefig.bbox' ]
2073
-
2074
2079
if bbox_inches :
2075
2080
if bbox_inches == "tight" :
2076
2081
renderer = _get_renderer (
@@ -2108,8 +2113,6 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
2108
2113
if bbox_inches and restore_bbox :
2109
2114
restore_bbox ()
2110
2115
2111
- self .figure .set_facecolor (origfacecolor )
2112
- self .figure .set_edgecolor (origedgecolor )
2113
2116
self .figure .set_canvas (self )
2114
2117
return result
2115
2118
0 commit comments