Thanks to visit codestin.com
Credit goes to github.com

Skip to content

savefig's pad_inches specifies per border padding #30183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/api/next_api_changes/behavior/30183-TS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
savefig() pad_inches per-border specification
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pad_inches will now accept a 4-tuple specifying the padding per border, in the order [left, right, bottom, top].
4 changes: 4 additions & 0 deletions doc/users/next_whats_new/pad_inches_per_border.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
savefig() pad_inches per-border specification
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

pad_inches will now accept a 4-tuple specifying the padding per border, in the order [left, right, bottom, top]. This can be used to simply and precisely modify the bounding box of what in a figure is saved to a file.
27 changes: 17 additions & 10 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -2088,11 +2088,11 @@
Bounding box in inches: only the given portion of the figure is
saved. If 'tight', try to figure out the tight bbox of the figure.

pad_inches : float or 'layout', default: :rc:`savefig.pad_inches`
Amount of padding in inches around the figure when bbox_inches is
'tight'. If 'layout' use the padding from the constrained or
compressed layout engine; ignored if one of those engines is not in
use.
pad_inches : float, 4-tuple of floats, or 'layout',
default: :rc:`savefig.pad_inches`. Amount of padding in inches
around the figure when bbox_inches is 'tight'. If 'layout' use the
padding from the constrained or compressed layout engine; ignored
if one of those engines is not in use.

bbox_extra_artists : list of `~matplotlib.artist.Artist`, optional
A list of extra artists that will be considered when the
Expand Down Expand Up @@ -2161,13 +2161,20 @@
renderer, bbox_extra_artists=bbox_extra_artists)
if (isinstance(layout_engine, ConstrainedLayoutEngine) and
pad_inches == "layout"):
h_pad = layout_engine.get()["h_pad"]
w_pad = layout_engine.get()["w_pad"]
b_pad = t_pad = layout_engine.get()["h_pad"]
l_pad = r_pad = layout_engine.get()["w_pad"]
else:
if pad_inches in [None, "layout"]:
if isinstance(pad_inches, (int, float)):
l_pad = r_pad = b_pad = t_pad = pad_inches
elif pad_inches in [None, "layout"]:
pad_inches = rcParams['savefig.pad_inches']
h_pad = w_pad = pad_inches
bbox_inches = bbox_inches.padded(w_pad, h_pad)
l_pad = r_pad = b_pad = t_pad = pad_inches
else:
l_pad = pad_inches[0]
r_pad = pad_inches[1]
b_pad = pad_inches[2]
t_pad = pad_inches[3]

Check warning on line 2176 in lib/matplotlib/backend_bases.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/backend_bases.py#L2173-L2176

Added lines #L2173 - L2176 were not covered by tests
bbox_inches = bbox_inches.padded_4sides(l_pad, r_pad, b_pad, t_pad)

# call adjust_bbox to save only the given area
restore_bbox = _tight_bbox.adjust_bbox(
Expand Down
10 changes: 5 additions & 5 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -3409,11 +3409,11 @@ def savefig(self, fname, *, transparent=None, **kwargs):
Bounding box in inches: only the given portion of the figure is
saved. If 'tight', try to figure out the tight bbox of the figure.

pad_inches : float or 'layout', default: :rc:`savefig.pad_inches`
Amount of padding in inches around the figure when bbox_inches is
'tight'. If 'layout' use the padding from the constrained or
compressed layout engine; ignored if one of those engines is not in
use.
pad_inches : float, 4-tuple of floats, or 'layout',
default: :rc:`savefig.pad_inches`. Amount of padding in inches
around the figure when bbox_inches is 'tight'. If 'layout' use the
padding from the constrained or compressed layout engine; ignored
if one of those engines is not in use.

facecolor : :mpltype:`color` or 'auto', default: :rc:`savefig.facecolor`
The facecolor of the figure. If 'auto', use the current figure
Expand Down
8 changes: 8 additions & 0 deletions lib/matplotlib/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,14 @@ def padded(self, w_pad, h_pad=None):
h_pad = w_pad
return Bbox(points + [[-w_pad, -h_pad], [w_pad, h_pad]])

def padded_4sides(self, l, r, b, t):
"""
Construct a `Bbox` by padding this one by a possibly different
amount on each side.
"""
points = self.get_points()
return Bbox(points + [[-l, -b], [r, t]])

def translated(self, tx, ty):
"""Construct a `Bbox` by translating this one by *tx* and *ty*."""
return Bbox(self._points + (tx, ty))
Expand Down
Loading