@@ -2196,24 +2196,27 @@ def print_figure(
21962196 self .figure .set_facecolor (facecolor )
21972197 self .figure .set_edgecolor (edgecolor )
21982198
2199- cl_state = self .figure .get_constrained_layout ()
2200-
22012199 if bbox_inches is None :
22022200 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+
22032218 if bbox_inches :
22042219 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-
22172220 bbox_inches = self .figure .get_tightbbox (
22182221 renderer , bbox_extra_artists = bbox_extra_artists )
22192222 if pad_inches is None :
@@ -2228,6 +2231,9 @@ def print_figure(
22282231 else :
22292232 _bbox_inches_restore = None
22302233
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 )
22312237 try :
22322238 result = print_method (
22332239 filename ,
@@ -2244,6 +2250,7 @@ def print_figure(
22442250 self .figure .set_facecolor (origfacecolor )
22452251 self .figure .set_edgecolor (origedgecolor )
22462252 self .figure .set_canvas (self )
2253+ # reset to cached state
22472254 self .figure .set_constrained_layout (cl_state )
22482255 return result
22492256
0 commit comments