3232 The base class for the messaging area.
3333"""
3434
35- from contextlib import contextmanager
35+ from contextlib import contextmanager , ExitStack
3636from enum import IntEnum
3737import functools
3838import 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
0 commit comments