4848from matplotlib ._pylab_helpers import Gcf
4949from matplotlib .backend_managers import ToolManager
5050from matplotlib .cbook import _setattr_cm
51+ from matplotlib .layout_engine import ConstrainedLayoutEngine
5152from matplotlib .path import Path
5253from matplotlib .texmanager import TexManager
5354from matplotlib .transforms import Affine2D
@@ -2273,8 +2274,11 @@ def print_figure(
22732274 Bounding box in inches: only the given portion of the figure is
22742275 saved. If 'tight', try to figure out the tight bbox of the figure.
22752276
2276- pad_inches : float, default: :rc:`savefig.pad_inches`
2277- Amount of padding around the figure when *bbox_inches* is 'tight'.
2277+ pad_inches : float or 'layout', default: :rc:`savefig.pad_inches`
2278+ Amount of padding in inches around the figure when bbox_inches is
2279+ 'tight'. If 'layout' use the padding from the constrained or
2280+ compressed layout engine; ignored if one of those engines is not in
2281+ use.
22782282
22792283 bbox_extra_artists : list of `~matplotlib.artist.Artist`, optional
22802284 A list of extra artists that will be considered when the
@@ -2324,8 +2328,8 @@ def print_figure(
23242328 if bbox_inches is None :
23252329 bbox_inches = rcParams ['savefig.bbox' ]
23262330
2327- if ( self .figure .get_layout_engine () is not None or
2328- bbox_inches == "tight" ) :
2331+ layout_engine = self .figure .get_layout_engine ()
2332+ if layout_engine is not None or bbox_inches == "tight" :
23292333 # we need to trigger a draw before printing to make sure
23302334 # CL works. "tight" also needs a draw to get the right
23312335 # locations:
@@ -2341,9 +2345,15 @@ def print_figure(
23412345 if bbox_inches == "tight" :
23422346 bbox_inches = self .figure .get_tightbbox (
23432347 renderer , bbox_extra_artists = bbox_extra_artists )
2344- if pad_inches is None :
2345- pad_inches = rcParams ['savefig.pad_inches' ]
2346- bbox_inches = bbox_inches .padded (pad_inches )
2348+ if (isinstance (layout_engine , ConstrainedLayoutEngine ) and
2349+ pad_inches == "layout" ):
2350+ h_pad = layout_engine .get ()["h_pad" ]
2351+ w_pad = layout_engine .get ()["w_pad" ]
2352+ else :
2353+ if pad_inches in [None , "layout" ]:
2354+ pad_inches = rcParams ['savefig.pad_inches' ]
2355+ h_pad = w_pad = pad_inches
2356+ bbox_inches = bbox_inches .padded (w_pad , h_pad )
23472357
23482358 # call adjust_bbox to save only the given area
23492359 restore_bbox = _tight_bbox .adjust_bbox (
0 commit comments