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