@@ -2145,17 +2145,6 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
2145
2145
tight bbox is calculated.
2146
2146
2147
2147
"""
2148
- self ._is_saving = True
2149
- # Remove the figure manager, if any, to avoid resizing the GUI widget.
2150
- # Having *no* manager and a *None* manager are currently different (see
2151
- # Figure.show); should probably be normalized to None at some point.
2152
- _no_manager = object ()
2153
- if hasattr (self , 'manager' ):
2154
- manager = self .manager
2155
- del self .manager
2156
- else :
2157
- manager = _no_manager
2158
-
2159
2148
if format is None :
2160
2149
# get format from filename, or from backend's default filetype
2161
2150
if isinstance (filename , six .string_types ):
@@ -2172,104 +2161,107 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
2172
2161
2173
2162
if dpi is None :
2174
2163
dpi = rcParams ['savefig.dpi' ]
2175
-
2176
2164
if dpi == 'figure' :
2177
2165
dpi = getattr (self .figure , '_original_dpi' , self .figure .dpi )
2178
2166
2179
- if facecolor is None :
2180
- facecolor = rcParams ['savefig.facecolor' ]
2181
- if edgecolor is None :
2182
- edgecolor = rcParams ['savefig.edgecolor' ]
2183
-
2184
- origDPI = self .figure .dpi
2185
- origfacecolor = self .figure .get_facecolor ()
2186
- origedgecolor = self .figure .get_edgecolor ()
2187
-
2188
- self .figure .dpi = dpi
2189
- self .figure .set_facecolor (facecolor )
2190
- self .figure .set_edgecolor (edgecolor )
2191
-
2192
- bbox_inches = kwargs .pop ("bbox_inches" , None )
2193
- if bbox_inches is None :
2194
- bbox_inches = rcParams ['savefig.bbox' ]
2195
-
2196
- if bbox_inches :
2197
- # call adjust_bbox to save only the given area
2198
- if bbox_inches == "tight" :
2199
- # When bbox_inches == "tight", it saves the figure twice. The
2200
- # first save command (to a BytesIO) is just to estimate the
2201
- # bounding box of the figure.
2167
+ # Remove the figure manager, if any, to avoid resizing the GUI widget.
2168
+ # Some code (e.g. Figure.show) differentiates between having *no*
2169
+ # manager and a *None* manager, which should be fixed at some point,
2170
+ # but this should be fine.
2171
+ with cbook ._setattr_cm (self , _is_saving = True , manager = None ), \
2172
+ cbook ._setattr_cm (self .figure , dpi = dpi ):
2173
+
2174
+ if facecolor is None :
2175
+ facecolor = rcParams ['savefig.facecolor' ]
2176
+ if edgecolor is None :
2177
+ edgecolor = rcParams ['savefig.edgecolor' ]
2178
+
2179
+ origfacecolor = self .figure .get_facecolor ()
2180
+ origedgecolor = self .figure .get_edgecolor ()
2181
+
2182
+ self .figure .dpi = dpi
2183
+ self .figure .set_facecolor (facecolor )
2184
+ self .figure .set_edgecolor (edgecolor )
2185
+
2186
+ bbox_inches = kwargs .pop ("bbox_inches" , None )
2187
+ if bbox_inches is None :
2188
+ bbox_inches = rcParams ['savefig.bbox' ]
2189
+
2190
+ if bbox_inches :
2191
+ # call adjust_bbox to save only the given area
2192
+ if bbox_inches == "tight" :
2193
+ # When bbox_inches == "tight", it saves the figure twice.
2194
+ # The first save command (to a BytesIO) is just to estimate
2195
+ # the bounding box of the figure.
2196
+ result = print_method (
2197
+ io .BytesIO (),
2198
+ dpi = dpi ,
2199
+ facecolor = facecolor ,
2200
+ edgecolor = edgecolor ,
2201
+ orientation = orientation ,
2202
+ dryrun = True ,
2203
+ ** kwargs )
2204
+ renderer = self .figure ._cachedRenderer
2205
+ bbox_inches = self .figure .get_tightbbox (renderer )
2206
+
2207
+ bbox_artists = kwargs .pop ("bbox_extra_artists" , None )
2208
+ if bbox_artists is None :
2209
+ bbox_artists = \
2210
+ self .figure .get_default_bbox_extra_artists ()
2211
+
2212
+ bbox_filtered = []
2213
+ for a in bbox_artists :
2214
+ bbox = a .get_window_extent (renderer )
2215
+ if a .get_clip_on ():
2216
+ clip_box = a .get_clip_box ()
2217
+ if clip_box is not None :
2218
+ bbox = Bbox .intersection (bbox , clip_box )
2219
+ clip_path = a .get_clip_path ()
2220
+ if clip_path is not None and bbox is not None :
2221
+ clip_path = \
2222
+ clip_path .get_fully_transformed_path ()
2223
+ bbox = Bbox .intersection (
2224
+ bbox , clip_path .get_extents ())
2225
+ if bbox is not None and (
2226
+ bbox .width != 0 or bbox .height != 0 ):
2227
+ bbox_filtered .append (bbox )
2228
+
2229
+ if bbox_filtered :
2230
+ _bbox = Bbox .union (bbox_filtered )
2231
+ trans = Affine2D ().scale (1.0 / self .figure .dpi )
2232
+ bbox_extra = TransformedBbox (_bbox , trans )
2233
+ bbox_inches = Bbox .union ([bbox_inches , bbox_extra ])
2234
+
2235
+ pad = kwargs .pop ("pad_inches" , None )
2236
+ if pad is None :
2237
+ pad = rcParams ['savefig.pad_inches' ]
2238
+
2239
+ bbox_inches = bbox_inches .padded (pad )
2240
+
2241
+ restore_bbox = tight_bbox .adjust_bbox (self .figure , bbox_inches ,
2242
+ canvas .fixed_dpi )
2243
+
2244
+ _bbox_inches_restore = (bbox_inches , restore_bbox )
2245
+ else :
2246
+ _bbox_inches_restore = None
2247
+
2248
+ try :
2202
2249
result = print_method (
2203
- io . BytesIO () ,
2250
+ filename ,
2204
2251
dpi = dpi ,
2205
2252
facecolor = facecolor ,
2206
2253
edgecolor = edgecolor ,
2207
2254
orientation = orientation ,
2208
- dryrun = True ,
2255
+ bbox_inches_restore = _bbox_inches_restore ,
2209
2256
** kwargs )
2210
- renderer = self .figure ._cachedRenderer
2211
- bbox_inches = self .figure .get_tightbbox (renderer )
2212
-
2213
- bbox_artists = kwargs .pop ("bbox_extra_artists" , None )
2214
- if bbox_artists is None :
2215
- bbox_artists = self .figure .get_default_bbox_extra_artists ()
2216
-
2217
- bbox_filtered = []
2218
- for a in bbox_artists :
2219
- bbox = a .get_window_extent (renderer )
2220
- if a .get_clip_on ():
2221
- clip_box = a .get_clip_box ()
2222
- if clip_box is not None :
2223
- bbox = Bbox .intersection (bbox , clip_box )
2224
- clip_path = a .get_clip_path ()
2225
- if clip_path is not None and bbox is not None :
2226
- clip_path = clip_path .get_fully_transformed_path ()
2227
- bbox = Bbox .intersection (bbox ,
2228
- clip_path .get_extents ())
2229
- if bbox is not None and (bbox .width != 0 or
2230
- bbox .height != 0 ):
2231
- bbox_filtered .append (bbox )
2232
-
2233
- if bbox_filtered :
2234
- _bbox = Bbox .union (bbox_filtered )
2235
- trans = Affine2D ().scale (1.0 / self .figure .dpi )
2236
- bbox_extra = TransformedBbox (_bbox , trans )
2237
- bbox_inches = Bbox .union ([bbox_inches , bbox_extra ])
2238
-
2239
- pad = kwargs .pop ("pad_inches" , None )
2240
- if pad is None :
2241
- pad = rcParams ['savefig.pad_inches' ]
2242
-
2243
- bbox_inches = bbox_inches .padded (pad )
2244
-
2245
- restore_bbox = tight_bbox .adjust_bbox (self .figure , bbox_inches ,
2246
- canvas .fixed_dpi )
2247
-
2248
- _bbox_inches_restore = (bbox_inches , restore_bbox )
2249
- else :
2250
- _bbox_inches_restore = None
2251
-
2252
- try :
2253
- result = print_method (
2254
- filename ,
2255
- dpi = dpi ,
2256
- facecolor = facecolor ,
2257
- edgecolor = edgecolor ,
2258
- orientation = orientation ,
2259
- bbox_inches_restore = _bbox_inches_restore ,
2260
- ** kwargs )
2261
- finally :
2262
- if bbox_inches and restore_bbox :
2263
- restore_bbox ()
2264
-
2265
- self .figure .dpi = origDPI
2266
- self .figure .set_facecolor (origfacecolor )
2267
- self .figure .set_edgecolor (origedgecolor )
2268
- self .figure .set_canvas (self )
2269
- if manager is not _no_manager :
2270
- self .manager = manager
2271
- self ._is_saving = False
2272
- return result
2257
+ finally :
2258
+ if bbox_inches and restore_bbox :
2259
+ restore_bbox ()
2260
+
2261
+ self .figure .set_facecolor (origfacecolor )
2262
+ self .figure .set_edgecolor (origedgecolor )
2263
+ self .figure .set_canvas (self )
2264
+ return result
2273
2265
2274
2266
@classmethod
2275
2267
def get_default_filetype (cls ):
0 commit comments