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
@@ -2001,32 +2001,35 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
20012001 can also be a file object on image backends
20022002
20032003 orientation : {'landscape', 'portrait'}, optional
2004- only currently applies to PostScript printing.
2004+ Only currently applies to PostScript printing.
20052005
20062006 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`.
20082009
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`.
20112013
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`.
20142017
20152018 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.
20172020
20182021 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`.
20222025
20232026 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`.
20262029
20272030 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.
20302033
20312034 """
20322035 if format is None :
@@ -2050,27 +2053,29 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
20502053 if dpi == 'figure' :
20512054 dpi = getattr (self .figure , '_original_dpi' , self .figure .dpi )
20522055
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 ))
20602064 if facecolor is None :
20612065 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 )
20622070 if edgecolor is None :
20632071 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 )
20702076
20712077 if bbox_inches is None :
20722078 bbox_inches = rcParams ['savefig.bbox' ]
2073-
20742079 if bbox_inches :
20752080 if bbox_inches == "tight" :
20762081 renderer = _get_renderer (
@@ -2108,8 +2113,6 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
21082113 if bbox_inches and restore_bbox :
21092114 restore_bbox ()
21102115
2111- self .figure .set_facecolor (origfacecolor )
2112- self .figure .set_edgecolor (origedgecolor )
21132116 self .figure .set_canvas (self )
21142117 return result
21152118
0 commit comments