@@ -2198,19 +2198,25 @@ def print_figure(
2198
2198
2199
2199
if bbox_inches is None :
2200
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
+
2201
2218
if bbox_inches :
2202
2219
if bbox_inches == "tight" :
2203
- renderer = _get_renderer (
2204
- self .figure ,
2205
- functools .partial (
2206
- print_method , orientation = orientation )
2207
- )
2208
- ctx = (renderer ._draw_disabled ()
2209
- if hasattr (renderer , '_draw_disabled' )
2210
- else suppress ())
2211
- with ctx :
2212
- self .figure .draw (renderer )
2213
-
2214
2220
bbox_inches = self .figure .get_tightbbox (
2215
2221
renderer , bbox_extra_artists = bbox_extra_artists )
2216
2222
if pad_inches is None :
@@ -2225,6 +2231,9 @@ def print_figure(
2225
2231
else :
2226
2232
_bbox_inches_restore = None
2227
2233
2234
+ # we have already done CL above, so turn it off:
2235
+ cl_state = self .figure .get_constrained_layout ()
2236
+ self .figure .set_constrained_layout (False )
2228
2237
try :
2229
2238
result = print_method (
2230
2239
filename ,
@@ -2241,6 +2250,8 @@ def print_figure(
2241
2250
self .figure .set_facecolor (origfacecolor )
2242
2251
self .figure .set_edgecolor (origedgecolor )
2243
2252
self .figure .set_canvas (self )
2253
+ # reset to cached state
2254
+ self .figure .set_constrained_layout (cl_state )
2244
2255
return result
2245
2256
2246
2257
@classmethod
0 commit comments