48
48
from matplotlib ._pylab_helpers import Gcf
49
49
from matplotlib .backend_managers import ToolManager
50
50
from matplotlib .cbook import _setattr_cm
51
+ from matplotlib .layout_engine import ConstrainedLayoutEngine
51
52
from matplotlib .path import Path
52
53
from matplotlib .texmanager import TexManager
53
54
from matplotlib .transforms import Affine2D
@@ -2273,8 +2274,11 @@ def print_figure(
2273
2274
Bounding box in inches: only the given portion of the figure is
2274
2275
saved. If 'tight', try to figure out the tight bbox of the figure.
2275
2276
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.
2278
2282
2279
2283
bbox_extra_artists : list of `~matplotlib.artist.Artist`, optional
2280
2284
A list of extra artists that will be considered when the
@@ -2324,8 +2328,8 @@ def print_figure(
2324
2328
if bbox_inches is None :
2325
2329
bbox_inches = rcParams ['savefig.bbox' ]
2326
2330
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" :
2329
2333
# we need to trigger a draw before printing to make sure
2330
2334
# CL works. "tight" also needs a draw to get the right
2331
2335
# locations:
@@ -2341,9 +2345,15 @@ def print_figure(
2341
2345
if bbox_inches == "tight" :
2342
2346
bbox_inches = self .figure .get_tightbbox (
2343
2347
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 )
2347
2357
2348
2358
# call adjust_bbox to save only the given area
2349
2359
restore_bbox = _tight_bbox .adjust_bbox (
0 commit comments