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