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
@@ -2002,32 +2002,35 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
20022002 can also be a file object on image backends
20032003
20042004 orientation : {'landscape', 'portrait'}, optional
2005- only currently applies to PostScript printing.
2005+ Only currently applies to PostScript printing.
20062006
20072007 dpi : scalar, optional
2008- the dots per inch to save the figure in; if None, use savefig.dpi
2008+ The dots per inch to save the figure in; if None, use
2009+ :rc:`savefig.dpi`.
20092010
2010- facecolor : color or None, optional
2011- the facecolor of the figure; if None, defaults to savefig.facecolor
2011+ facecolor : color or 'auto' or None, optional
2012+ The facecolor of the figure. If 'auto', use the current figure
2013+ facecolor. If None, use :rc:`savefig.facecolor`.
20122014
2013- edgecolor : color or None, optional
2014- the edgecolor of the figure; if None, defaults to savefig.edgecolor
2015+ edgecolor : color or 'auto' or None, optional
2016+ The edgecolor of the figure. If 'auto', use the current figure
2017+ edgecolor. If None, use :rc:`savefig.edgecolor`.
20152018
20162019 format : str, optional
2017- when set, forcibly set the file format to save to
2020+ When set, forcibly set the file format to save to.
20182021
20192022 bbox_inches : str or `~matplotlib.transforms.Bbox`, optional
2020- Bbox in inches. Only the given portion of the figure is
2021- saved. If 'tight', try to figure out the tight bbox of
2022- the figure. If None, use savefig.bbox
2023+ Bbox in inches. Only the given portion of the figure is saved. If
2024+ 'tight', try to figure out the tight bbox of the figure. If None,
2025+ use :rc:` savefig.bbox`.
20232026
20242027 pad_inches : scalar, optional
2025- Amount of padding around the figure when bbox_inches is
2026- 'tight'. If None, use savefig.pad_inches
2028+ Amount of padding around the figure when bbox_inches is 'tight'. If
2029+ None, use :rc:` savefig.pad_inches`.
20272030
20282031 bbox_extra_artists : list of `~matplotlib.artist.Artist`, optional
2029- A list of extra artists that will be considered when the
2030- tight bbox is calculated.
2032+ A list of extra artists that will be considered when the tight bbox
2033+ is calculated.
20312034
20322035 """
20332036 if format is None :
@@ -2051,27 +2054,29 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
20512054 if dpi == 'figure' :
20522055 dpi = getattr (self .figure , '_original_dpi' , self .figure .dpi )
20532056
2054- # Remove the figure manager, if any, to avoid resizing the GUI widget.
2055- # Some code (e.g. Figure.show) differentiates between having *no*
2056- # manager and a *None* manager, which should be fixed at some point,
2057- # but this should be fine.
2058- with cbook ._setattr_cm (self , _is_saving = True , manager = None ), \
2059- cbook ._setattr_cm (self .figure , dpi = dpi ):
2060-
2057+ with ExitStack () as stack :
2058+ # Remove the figure manager, if any, to avoid resizing the GUI
2059+ # widget. Some code (e.g. Figure.show) differentiates between
2060+ # having *no* manager and a *None* manager, which should be fixed
2061+ # at some point, but this should be fine.
2062+ stack .enter_context (
2063+ cbook ._setattr_cm (self , _is_saving = True , manager = None ))
2064+ stack .enter_context (cbook ._setattr_cm (self .figure , dpi = dpi ))
20612065 if facecolor is None :
20622066 facecolor = rcParams ['savefig.facecolor' ]
2067+ if not cbook ._str_equal (facecolor , 'auto' ):
2068+ stack .callback (
2069+ self .figure .set_facecolor , self .figure .get_facecolor ())
2070+ self .figure .set_facecolor (facecolor )
20632071 if edgecolor is None :
20642072 edgecolor = rcParams ['savefig.edgecolor' ]
2065-
2066- origfacecolor = self .figure .get_facecolor ()
2067- origedgecolor = self .figure .get_edgecolor ()
2068-
2069- self .figure .set_facecolor (facecolor )
2070- self .figure .set_edgecolor (edgecolor )
2073+ if not cbook ._str_equal (edgecolor , 'auto' ):
2074+ stack .callback (
2075+ self .figure .set_edgecolor , self .figure .get_edgecolor ())
2076+ self .figure .set_edgecolor (edgecolor )
20712077
20722078 if bbox_inches is None :
20732079 bbox_inches = rcParams ['savefig.bbox' ]
2074-
20752080 if bbox_inches :
20762081 if bbox_inches == "tight" :
20772082 renderer = _get_renderer (
@@ -2109,8 +2114,6 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
21092114 if bbox_inches and restore_bbox :
21102115 restore_bbox ()
21112116
2112- self .figure .set_facecolor (origfacecolor )
2113- self .figure .set_edgecolor (origedgecolor )
21142117 self .figure .set_canvas (self )
21152118 return result
21162119
0 commit comments