Skip to content

Fix get_constrained_layout_pads #25964

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

Merged
merged 2 commits into from
May 25, 2023
Merged
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
2 changes: 1 addition & 1 deletion lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -2872,7 +2872,7 @@ def get_constrained_layout_pads(self, relative=False):
"""
if not isinstance(self.get_layout_engine(), ConstrainedLayoutEngine):
return None, None, None, None
info = self.get_layout_engine().get_info()
info = self.get_layout_engine().get()
w_pad = info['w_pad']
h_pad = info['h_pad']
wspace = info['wspace']
Expand Down
23 changes: 22 additions & 1 deletion lib/matplotlib/layout_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ def __init__(self, **kwargs):
self._params = {}

def set(self, **kwargs):
"""
Set the parameters for the layout engine.
"""
raise NotImplementedError

@property
Expand Down Expand Up @@ -120,6 +123,9 @@ def __init__(self, adjust_compatible, colorbar_gridspec, **kwargs):
super().__init__(**kwargs)

def execute(self, fig):
"""
Do nothing.
"""
return


Expand All @@ -138,7 +144,7 @@ def __init__(self, *, pad=1.08, h_pad=None, w_pad=None,

Parameters
----------
pad : float, 1.08
pad : float, default: 1.08
Padding between the figure edge and the edges of subplots, as a
fraction of the font size.
h_pad, w_pad : float
Expand Down Expand Up @@ -182,6 +188,21 @@ def execute(self, fig):
fig.subplots_adjust(**kwargs)

def set(self, *, pad=None, w_pad=None, h_pad=None, rect=None):
"""
Set the pads for tight_layout.

Parameters
----------
pad : float
Padding between the figure edge and the edges of subplots, as a
fraction of the font size.
w_pad, h_pad : float
Padding (width/height) between edges of adjacent subplots.
Defaults to *pad*.
rect : tuple (left, bottom, right, top)
rectangle in normalized figure coordinates that the subplots
(including labels) will fit into.
"""
for td in self.set.__kwdefaults__:
if locals()[td] is not None:
self._params[td] = locals()[td]
Expand Down
8 changes: 8 additions & 0 deletions lib/matplotlib/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1603,3 +1603,11 @@ def test_savefig_metadata(fmt):
def test_savefig_metadata_error(fmt):
with pytest.raises(ValueError, match="metadata not supported"):
Figure().savefig(io.BytesIO(), format=fmt, metadata={})


def test_get_constrained_layout_pads():
params = {'w_pad': 0.01, 'h_pad': 0.02, 'wspace': 0.03, 'hspace': 0.04}
expected = tuple([*params.values()])
fig = plt.figure(layout=mpl.layout_engine.ConstrainedLayoutEngine(**params))
with pytest.warns(PendingDeprecationWarning, match="will be deprecated"):
assert fig.get_constrained_layout_pads() == expected