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