@@ -2196,24 +2196,27 @@ def print_figure(
2196
2196
self .figure .set_facecolor (facecolor )
2197
2197
self .figure .set_edgecolor (edgecolor )
2198
2198
2199
- cl_state = self .figure .get_constrained_layout ()
2200
-
2201
2199
if bbox_inches is None :
2202
2200
bbox_inches = rcParams ['savefig.bbox' ]
2201
+
2202
+ if (self .figure .get_constrained_layout () or
2203
+ bbox_inches == "tight" ):
2204
+ # we need to trigger a draw before printing to make sure
2205
+ # CL works. "tight" also needs a draw to get the right
2206
+ # locations:
2207
+ renderer = _get_renderer (
2208
+ self .figure ,
2209
+ functools .partial (
2210
+ print_method , orientation = orientation )
2211
+ )
2212
+ ctx = (renderer ._draw_disabled ()
2213
+ if hasattr (renderer , '_draw_disabled' )
2214
+ else suppress ())
2215
+ with ctx :
2216
+ self .figure .draw (renderer )
2217
+
2203
2218
if bbox_inches :
2204
2219
if bbox_inches == "tight" :
2205
- renderer = _get_renderer (
2206
- self .figure ,
2207
- functools .partial (
2208
- print_method , orientation = orientation )
2209
- )
2210
- ctx = (renderer ._draw_disabled ()
2211
- if hasattr (renderer , '_draw_disabled' )
2212
- else suppress ())
2213
- with ctx :
2214
- self .figure .draw (renderer )
2215
- self .figure .set_constrained_layout (False )
2216
-
2217
2220
bbox_inches = self .figure .get_tightbbox (
2218
2221
renderer , bbox_extra_artists = bbox_extra_artists )
2219
2222
if pad_inches is None :
@@ -2229,6 +2232,9 @@ def print_figure(
2229
2232
_bbox_inches_restore = None
2230
2233
2231
2234
try :
2235
+ # we have already done CL above, so turn it off:
2236
+ cl_state = self .figure .get_constrained_layout ()
2237
+ self .figure .set_constrained_layout (False )
2232
2238
result = print_method (
2233
2239
filename ,
2234
2240
dpi = dpi ,
@@ -2244,6 +2250,7 @@ def print_figure(
2244
2250
self .figure .set_facecolor (origfacecolor )
2245
2251
self .figure .set_edgecolor (origedgecolor )
2246
2252
self .figure .set_canvas (self )
2253
+ # reset to cached state
2247
2254
self .figure .set_constrained_layout (cl_state )
2248
2255
return result
2249
2256
0 commit comments