@@ -2113,17 +2113,6 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
21132113 tight bbox is calculated.
21142114
21152115 """
2116- self ._is_saving = True
2117- # Remove the figure manager, if any, to avoid resizing the GUI widget.
2118- # Having *no* manager and a *None* manager are currently different (see
2119- # Figure.show); should probably be normalized to None at some point.
2120- _no_manager = object ()
2121- if hasattr (self , 'manager' ):
2122- manager = self .manager
2123- del self .manager
2124- else :
2125- manager = _no_manager
2126-
21272116 if format is None :
21282117 # get format from filename, or from backend's default filetype
21292118 if isinstance (filename , getattr (os , "PathLike" , ())):
@@ -2142,104 +2131,107 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
21422131
21432132 if dpi is None :
21442133 dpi = rcParams ['savefig.dpi' ]
2145-
21462134 if dpi == 'figure' :
21472135 dpi = getattr (self .figure , '_original_dpi' , self .figure .dpi )
21482136
2149- if facecolor is None :
2150- facecolor = rcParams ['savefig.facecolor' ]
2151- if edgecolor is None :
2152- edgecolor = rcParams ['savefig.edgecolor' ]
2153-
2154- origDPI = self .figure .dpi
2155- origfacecolor = self .figure .get_facecolor ()
2156- origedgecolor = self .figure .get_edgecolor ()
2157-
2158- self .figure .dpi = dpi
2159- self .figure .set_facecolor (facecolor )
2160- self .figure .set_edgecolor (edgecolor )
2161-
2162- bbox_inches = kwargs .pop ("bbox_inches" , None )
2163- if bbox_inches is None :
2164- bbox_inches = rcParams ['savefig.bbox' ]
2165-
2166- if bbox_inches :
2167- # call adjust_bbox to save only the given area
2168- if bbox_inches == "tight" :
2169- # When bbox_inches == "tight", it saves the figure twice. The
2170- # first save command (to a BytesIO) is just to estimate the
2171- # bounding box of the figure.
2137+ # Remove the figure manager, if any, to avoid resizing the GUI widget.
2138+ # Some code (e.g. Figure.show) differentiates between having *no*
2139+ # manager and a *None* manager, which should be fixed at some point,
2140+ # but this should be fine.
2141+ with cbook ._setattr_cm (self , _is_saving = True , manager = None ), \
2142+ cbook ._setattr_cm (self .figure , dpi = dpi ):
2143+
2144+ if facecolor is None :
2145+ facecolor = rcParams ['savefig.facecolor' ]
2146+ if edgecolor is None :
2147+ edgecolor = rcParams ['savefig.edgecolor' ]
2148+
2149+ origfacecolor = self .figure .get_facecolor ()
2150+ origedgecolor = self .figure .get_edgecolor ()
2151+
2152+ self .figure .dpi = dpi
2153+ self .figure .set_facecolor (facecolor )
2154+ self .figure .set_edgecolor (edgecolor )
2155+
2156+ bbox_inches = kwargs .pop ("bbox_inches" , None )
2157+ if bbox_inches is None :
2158+ bbox_inches = rcParams ['savefig.bbox' ]
2159+
2160+ if bbox_inches :
2161+ # call adjust_bbox to save only the given area
2162+ if bbox_inches == "tight" :
2163+ # When bbox_inches == "tight", it saves the figure twice.
2164+ # The first save command (to a BytesIO) is just to estimate
2165+ # the bounding box of the figure.
2166+ result = print_method (
2167+ io .BytesIO (),
2168+ dpi = dpi ,
2169+ facecolor = facecolor ,
2170+ edgecolor = edgecolor ,
2171+ orientation = orientation ,
2172+ dryrun = True ,
2173+ ** kwargs )
2174+ renderer = self .figure ._cachedRenderer
2175+ bbox_inches = self .figure .get_tightbbox (renderer )
2176+
2177+ bbox_artists = kwargs .pop ("bbox_extra_artists" , None )
2178+ if bbox_artists is None :
2179+ bbox_artists = \
2180+ self .figure .get_default_bbox_extra_artists ()
2181+
2182+ bbox_filtered = []
2183+ for a in bbox_artists :
2184+ bbox = a .get_window_extent (renderer )
2185+ if a .get_clip_on ():
2186+ clip_box = a .get_clip_box ()
2187+ if clip_box is not None :
2188+ bbox = Bbox .intersection (bbox , clip_box )
2189+ clip_path = a .get_clip_path ()
2190+ if clip_path is not None and bbox is not None :
2191+ clip_path = \
2192+ clip_path .get_fully_transformed_path ()
2193+ bbox = Bbox .intersection (
2194+ bbox , clip_path .get_extents ())
2195+ if bbox is not None and (
2196+ bbox .width != 0 or bbox .height != 0 ):
2197+ bbox_filtered .append (bbox )
2198+
2199+ if bbox_filtered :
2200+ _bbox = Bbox .union (bbox_filtered )
2201+ trans = Affine2D ().scale (1.0 / self .figure .dpi )
2202+ bbox_extra = TransformedBbox (_bbox , trans )
2203+ bbox_inches = Bbox .union ([bbox_inches , bbox_extra ])
2204+
2205+ pad = kwargs .pop ("pad_inches" , None )
2206+ if pad is None :
2207+ pad = rcParams ['savefig.pad_inches' ]
2208+
2209+ bbox_inches = bbox_inches .padded (pad )
2210+
2211+ restore_bbox = tight_bbox .adjust_bbox (self .figure , bbox_inches ,
2212+ canvas .fixed_dpi )
2213+
2214+ _bbox_inches_restore = (bbox_inches , restore_bbox )
2215+ else :
2216+ _bbox_inches_restore = None
2217+
2218+ try :
21722219 result = print_method (
2173- io . BytesIO () ,
2220+ filename ,
21742221 dpi = dpi ,
21752222 facecolor = facecolor ,
21762223 edgecolor = edgecolor ,
21772224 orientation = orientation ,
2178- dryrun = True ,
2225+ bbox_inches_restore = _bbox_inches_restore ,
21792226 ** kwargs )
2180- renderer = self .figure ._cachedRenderer
2181- bbox_inches = self .figure .get_tightbbox (renderer )
2182-
2183- bbox_artists = kwargs .pop ("bbox_extra_artists" , None )
2184- if bbox_artists is None :
2185- bbox_artists = self .figure .get_default_bbox_extra_artists ()
2186-
2187- bbox_filtered = []
2188- for a in bbox_artists :
2189- bbox = a .get_window_extent (renderer )
2190- if a .get_clip_on ():
2191- clip_box = a .get_clip_box ()
2192- if clip_box is not None :
2193- bbox = Bbox .intersection (bbox , clip_box )
2194- clip_path = a .get_clip_path ()
2195- if clip_path is not None and bbox is not None :
2196- clip_path = clip_path .get_fully_transformed_path ()
2197- bbox = Bbox .intersection (bbox ,
2198- clip_path .get_extents ())
2199- if bbox is not None and (bbox .width != 0 or
2200- bbox .height != 0 ):
2201- bbox_filtered .append (bbox )
2202-
2203- if bbox_filtered :
2204- _bbox = Bbox .union (bbox_filtered )
2205- trans = Affine2D ().scale (1.0 / self .figure .dpi )
2206- bbox_extra = TransformedBbox (_bbox , trans )
2207- bbox_inches = Bbox .union ([bbox_inches , bbox_extra ])
2208-
2209- pad = kwargs .pop ("pad_inches" , None )
2210- if pad is None :
2211- pad = rcParams ['savefig.pad_inches' ]
2212-
2213- bbox_inches = bbox_inches .padded (pad )
2214-
2215- restore_bbox = tight_bbox .adjust_bbox (self .figure , bbox_inches ,
2216- canvas .fixed_dpi )
2217-
2218- _bbox_inches_restore = (bbox_inches , restore_bbox )
2219- else :
2220- _bbox_inches_restore = None
2221-
2222- try :
2223- result = print_method (
2224- filename ,
2225- dpi = dpi ,
2226- facecolor = facecolor ,
2227- edgecolor = edgecolor ,
2228- orientation = orientation ,
2229- bbox_inches_restore = _bbox_inches_restore ,
2230- ** kwargs )
2231- finally :
2232- if bbox_inches and restore_bbox :
2233- restore_bbox ()
2234-
2235- self .figure .dpi = origDPI
2236- self .figure .set_facecolor (origfacecolor )
2237- self .figure .set_edgecolor (origedgecolor )
2238- self .figure .set_canvas (self )
2239- if manager is not _no_manager :
2240- self .manager = manager
2241- self ._is_saving = False
2242- return result
2227+ finally :
2228+ if bbox_inches and restore_bbox :
2229+ restore_bbox ()
2230+
2231+ self .figure .set_facecolor (origfacecolor )
2232+ self .figure .set_edgecolor (origedgecolor )
2233+ self .figure .set_canvas (self )
2234+ return result
22432235
22442236 @classmethod
22452237 def get_default_filetype (cls ):
0 commit comments