From 67d112685a503a620938ce6db5a5898201ff1e9c Mon Sep 17 00:00:00 2001 From: Steffen Rehberg Date: Wed, 24 May 2023 16:06:21 +0200 Subject: [PATCH 1/2] Fix get_constrained_layout_pads() - fix non-existing function get_info() - complete docs --- lib/matplotlib/figure.py | 2 +- lib/matplotlib/layout_engine.py | 23 ++++++++++++++++++++++- lib/matplotlib/tests/test_figure.py | 8 ++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 39517e14d0e3..08bb483d606b 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -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'] diff --git a/lib/matplotlib/layout_engine.py b/lib/matplotlib/layout_engine.py index 1989f89684f1..d751059f4e09 100644 --- a/lib/matplotlib/layout_engine.py +++ b/lib/matplotlib/layout_engine.py @@ -64,6 +64,9 @@ def __init__(self, **kwargs): self._params = {} def set(self, **kwargs): + """ + Set the parameters for the layout engine. + """ raise NotImplementedError @property @@ -120,6 +123,9 @@ def __init__(self, adjust_compatible, colorbar_gridspec, **kwargs): super().__init__(**kwargs) def execute(self, fig): + """ + Do nothing. + """ return @@ -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 @@ -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] diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index a795a3fbe3f5..06d594c4ea1a 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -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(): + expected = (0.01, 0.02, 0.03, 0.04) + params = dict(zip(['w_pad', 'h_pad', 'wspace', 'hspace'], expected)) + fig = plt.figure(layout=mpl.layout_engine.ConstrainedLayoutEngine(**params)) + with pytest.warns(PendingDeprecationWarning, match="will be deprecated"): + assert fig.get_constrained_layout_pads() == expected From b45d2c4cf94a3d3b471803f0f348151ba6ca4ae5 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Wed, 24 May 2023 16:29:56 -0400 Subject: [PATCH 2/2] MNT: minor change to test Co-authored-by: Elliott Sales de Andrade --- lib/matplotlib/tests/test_figure.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index 06d594c4ea1a..474331bf9149 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -1606,8 +1606,8 @@ def test_savefig_metadata_error(fmt): def test_get_constrained_layout_pads(): - expected = (0.01, 0.02, 0.03, 0.04) - params = dict(zip(['w_pad', 'h_pad', 'wspace', 'hspace'], expected)) + 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