From cf6032e51f4525609b2bb50a2dd41933bc87214c Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Tue, 24 Oct 2023 22:30:48 +0200 Subject: [PATCH 01/36] Fix behaviour of Figure.clear() for SubplotParams --- doc/users/next_whats_new/subplots_adjust.rst | 10 ++++ lib/matplotlib/figure.py | 9 +++- lib/matplotlib/gridspec.py | 51 ++++++++++++-------- lib/matplotlib/tests/test_figure.py | 41 ++++++++++++++++ 4 files changed, 90 insertions(+), 21 deletions(-) create mode 100644 doc/users/next_whats_new/subplots_adjust.rst diff --git a/doc/users/next_whats_new/subplots_adjust.rst b/doc/users/next_whats_new/subplots_adjust.rst new file mode 100644 index 000000000000..84977f6eb37e --- /dev/null +++ b/doc/users/next_whats_new/subplots_adjust.rst @@ -0,0 +1,10 @@ +subplots_adjust has a new ``kwarg``: ``rc_default`` +--------------------------------------------------- + +`.Figure.subplots_adjust` and `.pyplot.subplots_adjust` have a new ``kwarg``: +``rc_default`` that determines the default values for the subplot parameters. + +The `.figure.SubplotParams` object has a new get method +:meth:`~.SubplotParams.get_subplot_params` + +When calling `.Figure.clear()` the settings for `SubplotParams` are restored to the default values. diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 087c193d48c3..39f54bac26af 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -925,6 +925,7 @@ def clear(self, keep_observers=False): self.texts = [] self.images = [] self.legends = [] + self.subplotpars.update(rc_default=True) if not keep_observers: self._axobservers = cbook.CallbackRegistry() self._suptitle = None @@ -1250,7 +1251,7 @@ def colorbar( return cb def subplots_adjust(self, left=None, bottom=None, right=None, top=None, - wspace=None, hspace=None): + wspace=None, hspace=None, rc_default=False): """ Adjust the subplot layout parameters. @@ -1277,6 +1278,10 @@ def subplots_adjust(self, left=None, bottom=None, right=None, top=None, hspace : float, optional The height of the padding between subplots, as a fraction of the average Axes height. + rc_default : bool + Determine the defaults. *False*, the default, and the values + are unchanged. *True* and the values are taken from + :rc:`figure.subplot.*` """ if (self.get_layout_engine() is not None and not self.get_layout_engine().adjust_compatible): @@ -1285,7 +1290,7 @@ def subplots_adjust(self, left=None, bottom=None, right=None, top=None, "incompatible with subplots_adjust and/or tight_layout; " "not calling subplots_adjust.") return - self.subplotpars.update(left, bottom, right, top, wspace, hspace) + self.subplotpars.update(left, bottom, right, top, wspace, hspace, rc_default = rc_default) for ax in self.axes: if ax.get_subplotspec() is not None: ax._set_position(ax.get_subplotspec().get_position(self)) diff --git a/lib/matplotlib/gridspec.py b/lib/matplotlib/gridspec.py index c6b363d36efa..9084122d4f9b 100644 --- a/lib/matplotlib/gridspec.py +++ b/lib/matplotlib/gridspec.py @@ -728,6 +728,7 @@ def subgridspec(self, nrows, ncols, **kwargs): return GridSpecFromSubplotSpec(nrows, ncols, self, **kwargs) + class SubplotParams: """ Parameters defining the positioning of a subplots grid in a figure. @@ -740,22 +741,22 @@ def __init__(self, left=None, bottom=None, right=None, top=None, Parameters ---------- - left : float + left : float, optional The position of the left edge of the subplots, as a fraction of the figure width. - right : float + right : float, optional The position of the right edge of the subplots, as a fraction of the figure width. - bottom : float + bottom : float, optional The position of the bottom edge of the subplots, as a fraction of the figure height. - top : float + top : float, optional The position of the top edge of the subplots, as a fraction of the figure height. - wspace : float + wspace : float, optional The width of the padding between subplots, as a fraction of the average Axes width. - hspace : float + hspace : float, optional The height of the padding between subplots, as a fraction of the average Axes height. """ @@ -763,10 +764,16 @@ def __init__(self, left=None, bottom=None, right=None, top=None, setattr(self, key, mpl.rcParams[f"figure.subplot.{key}"]) self.update(left, bottom, right, top, wspace, hspace) + def _repr_pretty_(self, p: Any, cycle: bool) -> None: + del cycle + s = f"{self.__class__.__name__}(left={self.left}, bottom={self.bottom}, right={self.right}, top={self.top}, " + f"wspace={self.wspace}, hspace={self.hspace})\n" + p.text(s) + def update(self, left=None, bottom=None, right=None, top=None, - wspace=None, hspace=None): + wspace=None, hspace=None, rc_default=False): """ Update the dimensions of the passed parameters. *None* means unchanged. + If *rc_default* is True, then restore the parameters with value *None* to the default. """ if ((left if left is not None else self.left) >= (right if right is not None else self.right)): @@ -774,15 +781,21 @@ def update(self, left=None, bottom=None, right=None, top=None, if ((bottom if bottom is not None else self.bottom) >= (top if top is not None else self.top)): raise ValueError('bottom cannot be >= top') - if left is not None: - self.left = left - if right is not None: - self.right = right - if bottom is not None: - self.bottom = bottom - if top is not None: - self.top = top - if wspace is not None: - self.wspace = wspace - if hspace is not None: - self.hspace = hspace + + attributes = {'left': left, 'right': right, 'bottom': bottom, 'top': top, 'hspace': hspace, 'wspace': wspace} + for key, value in attributes.items(): + if value is None: + if rc_default: + setattr(self, key, mpl.rcParams[f'figure.subplot.{key}']) + else: + setattr(self, key, value) + + def get_subplot_params(self) -> dict[str, float]: + """ + Returns + ------- + subplot_params : dictionary + A dictionary with the subplot parameters + """ + return self.__dict__.copy() + \ No newline at end of file diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index c3d177425723..ab78c7884a56 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -1706,3 +1706,44 @@ def test_warn_colorbar_mismatch(): subfig3_1.colorbar(im3_2) # should not warn with pytest.warns(UserWarning, match="different Figure"): subfig3_1.colorbar(im4_1) + +def test_clf_subplotpars(): + keys = ('left', 'right', 'bottom', 'top', 'wspace', 'hspace') + rc_params = {key: plt.rcParams['figure.subplot.'+key] for key in keys} + + fig = plt.figure(1) + fig.subplots_adjust(left=0.1) + fig.clf() + assert fig.subplotpars.get_subplot_params() == rc_params + + +def test_suplots_adjust_1(): + fig = plt.figure(1) + wspace = 0 + fig.subplots_adjust(wspace=wspace) + inDict = dict(left=0.1, right=0.7, bottom=0, top=0.9, hspace=0.05) + fig.subplots_adjust(**inDict) + inDict['wspace'] = wspace + assert fig.subplotpars.get_subplot_params() == inDict + + +def test_suplots_adjust_2(): + fig = plt.figure(1) + fig.subplots_adjust(wspace=0) + inDict = dict(left=0.1, right=0.7, bottom=0, top=0.9, hspace=0.05, + rc_default=True) + fig.subplots_adjust(**inDict) + inDict['wspace'] = plt.rcParams['figure.subplot.wspace'] + del inDict['rc_default'] + assert fig.subplotpars.get_subplot_params() == inDict + + +def test_suplots_adjust_plt(): + plt.figure(1) + plt.subplots_adjust(wspace=0) + inDict = dict(left=0.1, right=0.7, bottom=0, top=0.9, hspace=0.05, + rc_default=True) + plt.subplots_adjust(**inDict) + inDict['wspace'] = plt.rcParams['figure.subplot.wspace'] + del inDict['rc_default'] + assert plt.gcf().subplotpars.get_subplot_params() == inDict From 68386fa747c500a854f1357399616161612df007 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Tue, 24 Oct 2023 22:32:46 +0200 Subject: [PATCH 02/36] update credits --- doc/users/next_whats_new/subplots_adjust.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/users/next_whats_new/subplots_adjust.rst b/doc/users/next_whats_new/subplots_adjust.rst index 84977f6eb37e..21e600b830cd 100644 --- a/doc/users/next_whats_new/subplots_adjust.rst +++ b/doc/users/next_whats_new/subplots_adjust.rst @@ -8,3 +8,5 @@ The `.figure.SubplotParams` object has a new get method :meth:`~.SubplotParams.get_subplot_params` When calling `.Figure.clear()` the settings for `SubplotParams` are restored to the default values. + +(code based on work by @fredrik-1) \ No newline at end of file From 035a9c039bd738eeb9dc116d3883b2ba5b56ead1 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Tue, 24 Oct 2023 22:39:36 +0200 Subject: [PATCH 03/36] lint --- lib/matplotlib/gridspec.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/gridspec.py b/lib/matplotlib/gridspec.py index 9084122d4f9b..77b224a93ba1 100644 --- a/lib/matplotlib/gridspec.py +++ b/lib/matplotlib/gridspec.py @@ -766,7 +766,8 @@ def __init__(self, left=None, bottom=None, right=None, top=None, def _repr_pretty_(self, p: Any, cycle: bool) -> None: del cycle - s = f"{self.__class__.__name__}(left={self.left}, bottom={self.bottom}, right={self.right}, top={self.top}, " + f"wspace={self.wspace}, hspace={self.hspace})\n" + s = f"{self.__class__.__name__}(left={self.left}, bottom={self.bottom}, right={self.right}, top={self.top}, " + s += f"wspace={self.wspace}, hspace={self.hspace})" p.text(s) def update(self, left=None, bottom=None, right=None, top=None, From c2c13ffd98cc6893ff23aff71163a67022e10e84 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Tue, 24 Oct 2023 22:45:02 +0200 Subject: [PATCH 04/36] lint --- lib/matplotlib/figure.py | 3 ++- lib/matplotlib/gridspec.py | 13 +++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 39f54bac26af..fa0f65fc3f76 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -1290,7 +1290,8 @@ def subplots_adjust(self, left=None, bottom=None, right=None, top=None, "incompatible with subplots_adjust and/or tight_layout; " "not calling subplots_adjust.") return - self.subplotpars.update(left, bottom, right, top, wspace, hspace, rc_default = rc_default) + self.subplotpars.update(left, bottom, right, top, wspace, hspace, + rc_default=rc_default) for ax in self.axes: if ax.get_subplotspec() is not None: ax._set_position(ax.get_subplotspec().get_position(self)) diff --git a/lib/matplotlib/gridspec.py b/lib/matplotlib/gridspec.py index 77b224a93ba1..739464fd419b 100644 --- a/lib/matplotlib/gridspec.py +++ b/lib/matplotlib/gridspec.py @@ -9,7 +9,7 @@ methods like `~.pyplot.subplots`, `~.pyplot.subplot_mosaic` and `~.Figure.subfigures`. See the tutorial :ref:`arranging_axes` for a guide. """ - +from typing import Any import copy import logging from numbers import Integral @@ -728,7 +728,6 @@ def subgridspec(self, nrows, ncols, **kwargs): return GridSpecFromSubplotSpec(nrows, ncols, self, **kwargs) - class SubplotParams: """ Parameters defining the positioning of a subplots grid in a figure. @@ -766,10 +765,11 @@ def __init__(self, left=None, bottom=None, right=None, top=None, def _repr_pretty_(self, p: Any, cycle: bool) -> None: del cycle - s = f"{self.__class__.__name__}(left={self.left}, bottom={self.bottom}, right={self.right}, top={self.top}, " - s += f"wspace={self.wspace}, hspace={self.hspace})" + name = self.__class__.__name__ + s = f"{name}(left={self.left}, bottom={self.bottom}, right={self.right}, " + s += f" top={self.top}, wspace={self.wspace}, hspace={self.hspace})" p.text(s) - + def update(self, left=None, bottom=None, right=None, top=None, wspace=None, hspace=None, rc_default=False): """ @@ -783,7 +783,8 @@ def update(self, left=None, bottom=None, right=None, top=None, >= (top if top is not None else self.top)): raise ValueError('bottom cannot be >= top') - attributes = {'left': left, 'right': right, 'bottom': bottom, 'top': top, 'hspace': hspace, 'wspace': wspace} + attributes = {'left': left, 'right': right, 'bottom': bottom, 'top': top, + 'hspace': hspace, 'wspace': wspace} for key, value in attributes.items(): if value is None: if rc_default: From 247833ce532bb9f21d27e7c4184ed7b277ac0ade Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Tue, 24 Oct 2023 22:45:34 +0200 Subject: [PATCH 05/36] lint --- lib/matplotlib/tests/test_figure.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index ab78c7884a56..ad02559abcee 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -1707,6 +1707,7 @@ def test_warn_colorbar_mismatch(): with pytest.warns(UserWarning, match="different Figure"): subfig3_1.colorbar(im4_1) + def test_clf_subplotpars(): keys = ('left', 'right', 'bottom', 'top', 'wspace', 'hspace') rc_params = {key: plt.rcParams['figure.subplot.'+key] for key in keys} From e2039f852777f4668a17165c48c4244ddcdb9f32 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Tue, 24 Oct 2023 22:53:26 +0200 Subject: [PATCH 06/36] lint --- doc/users/next_whats_new/subplots_adjust.rst | 6 +++--- lib/matplotlib/figure.pyi | 1 + lib/matplotlib/gridspec.py | 6 +++--- lib/matplotlib/gridspec.pyi | 4 ++++ 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/doc/users/next_whats_new/subplots_adjust.rst b/doc/users/next_whats_new/subplots_adjust.rst index 21e600b830cd..5226bd280f9d 100644 --- a/doc/users/next_whats_new/subplots_adjust.rst +++ b/doc/users/next_whats_new/subplots_adjust.rst @@ -1,12 +1,12 @@ subplots_adjust has a new ``kwarg``: ``rc_default`` --------------------------------------------------- -`.Figure.subplots_adjust` and `.pyplot.subplots_adjust` have a new ``kwarg``: +`.Figure.subplots_adjust` and `.pyplot.subplots_adjust` have a new ``kwarg``: ``rc_default`` that determines the default values for the subplot parameters. -The `.figure.SubplotParams` object has a new get method +The `.figure.SubplotParams` object has a new get method :meth:`~.SubplotParams.get_subplot_params` When calling `.Figure.clear()` the settings for `SubplotParams` are restored to the default values. -(code based on work by @fredrik-1) \ No newline at end of file +(code based on work by @fredrik-1) diff --git a/lib/matplotlib/figure.pyi b/lib/matplotlib/figure.pyi index 687ae9e500d0..e07389b410e9 100644 --- a/lib/matplotlib/figure.pyi +++ b/lib/matplotlib/figure.pyi @@ -158,6 +158,7 @@ class FigureBase(Artist): top: float | None = ..., wspace: float | None = ..., hspace: float | None = ..., + rc_default: bool = ..., ) -> None: ... def align_xlabels(self, axs: Iterable[Axes] | None = ...) -> None: ... def align_ylabels(self, axs: Iterable[Axes] | None = ...) -> None: ... diff --git a/lib/matplotlib/gridspec.py b/lib/matplotlib/gridspec.py index 739464fd419b..201e0831e72b 100644 --- a/lib/matplotlib/gridspec.py +++ b/lib/matplotlib/gridspec.py @@ -774,7 +774,8 @@ def update(self, left=None, bottom=None, right=None, top=None, wspace=None, hspace=None, rc_default=False): """ Update the dimensions of the passed parameters. *None* means unchanged. - If *rc_default* is True, then restore the parameters with value *None* to the default. + If *rc_default* is True, then restore the parameters with value *None* + to the default. """ if ((left if left is not None else self.left) >= (right if right is not None else self.right)): @@ -782,7 +783,7 @@ def update(self, left=None, bottom=None, right=None, top=None, if ((bottom if bottom is not None else self.bottom) >= (top if top is not None else self.top)): raise ValueError('bottom cannot be >= top') - + attributes = {'left': left, 'right': right, 'bottom': bottom, 'top': top, 'hspace': hspace, 'wspace': wspace} for key, value in attributes.items(): @@ -800,4 +801,3 @@ def get_subplot_params(self) -> dict[str, float]: A dictionary with the subplot parameters """ return self.__dict__.copy() - \ No newline at end of file diff --git a/lib/matplotlib/gridspec.pyi b/lib/matplotlib/gridspec.pyi index 1ac1bb0b40e7..2c4508b1baa8 100644 --- a/lib/matplotlib/gridspec.pyi +++ b/lib/matplotlib/gridspec.pyi @@ -157,4 +157,8 @@ class SubplotParams: top: float | None = ..., wspace: float | None = ..., hspace: float | None = ..., + rc_default: bool = ..., ) -> None: ... + def get_subplot_params( + self, + ) -> dict[str, float]: ... From 1b7681709caecb6c180096a8214ee35ddf7d9407 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Wed, 25 Oct 2023 11:06:29 +0200 Subject: [PATCH 07/36] run boilerplate.py --- lib/matplotlib/pyplot.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 04d889ec0616..aa312dd922ce 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -2604,9 +2604,16 @@ def subplots_adjust( top: float | None = None, wspace: float | None = None, hspace: float | None = None, + rc_default: bool = False, ) -> None: gcf().subplots_adjust( - left=left, bottom=bottom, right=right, top=top, wspace=wspace, hspace=hspace + left=left, + bottom=bottom, + right=right, + top=top, + wspace=wspace, + hspace=hspace, + rc_default=rc_default, ) From 51876fcced42a6ced8a845d99ff980dc808ad002 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Wed, 25 Oct 2023 14:59:57 +0200 Subject: [PATCH 08/36] add tests --- lib/matplotlib/tests/test_gridspec.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/matplotlib/tests/test_gridspec.py b/lib/matplotlib/tests/test_gridspec.py index deda73c3b6ab..08d31bcce465 100644 --- a/lib/matplotlib/tests/test_gridspec.py +++ b/lib/matplotlib/tests/test_gridspec.py @@ -1,3 +1,4 @@ +import unittest import matplotlib.gridspec as gridspec import matplotlib.pyplot as plt import pytest @@ -27,6 +28,14 @@ def test_height_ratios(): gridspec.GridSpec(1, 1, height_ratios=[2, 1, 3]) +def test_SubplotParams(): + s = gridspec.SubplotParams(.1, .1, .9, .9) + assert s.left == 0.1 + + with pytest.raises(ValueError): + s = gridspec.SubplotParams(.1, .1, .09, .9) + + def test_repr(): ss = gridspec.GridSpec(3, 3)[2, 1:3] assert repr(ss) == "GridSpec(3, 3)[2:3, 1:3]" @@ -48,3 +57,8 @@ def test_subplotspec_args(): gs = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=axs[0]) with pytest.raises(TypeError, match="subplot_spec must be type SubplotSpec"): gs = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=axs) + + s = gridspec.SubplotParams(.1, .1, .9, .9) + p = unittest.mock.MagicMock() + s._repr_pretty_(p, False) + assert 'SubplotParams' in p.method_calls[0].args[0] From 1470bf2e40d75ef8fc176e6768fda3ff3300368e Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Wed, 6 Dec 2023 19:35:43 +0100 Subject: [PATCH 09/36] Update doc/users/next_whats_new/subplots_adjust.rst Co-authored-by: Kyle Sunden --- doc/users/next_whats_new/subplots_adjust.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/users/next_whats_new/subplots_adjust.rst b/doc/users/next_whats_new/subplots_adjust.rst index 5226bd280f9d..eb8ef8b6bd26 100644 --- a/doc/users/next_whats_new/subplots_adjust.rst +++ b/doc/users/next_whats_new/subplots_adjust.rst @@ -4,7 +4,7 @@ subplots_adjust has a new ``kwarg``: ``rc_default`` `.Figure.subplots_adjust` and `.pyplot.subplots_adjust` have a new ``kwarg``: ``rc_default`` that determines the default values for the subplot parameters. -The `.figure.SubplotParams` object has a new get method +The `.gridspec.SubplotParams` object has a new get method :meth:`~.SubplotParams.get_subplot_params` When calling `.Figure.clear()` the settings for `SubplotParams` are restored to the default values. From 199cc4a809bae46ed8d2d029251a99820d13b3cb Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Wed, 6 Dec 2023 19:37:45 +0100 Subject: [PATCH 10/36] update news entry --- doc/users/next_whats_new/subplots_adjust.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/users/next_whats_new/subplots_adjust.rst b/doc/users/next_whats_new/subplots_adjust.rst index eb8ef8b6bd26..5a243eb8ccf7 100644 --- a/doc/users/next_whats_new/subplots_adjust.rst +++ b/doc/users/next_whats_new/subplots_adjust.rst @@ -7,6 +7,6 @@ subplots_adjust has a new ``kwarg``: ``rc_default`` The `.gridspec.SubplotParams` object has a new get method :meth:`~.SubplotParams.get_subplot_params` -When calling `.Figure.clear()` the settings for `SubplotParams` are restored to the default values. +When calling `.Figure.clear()` the settings for `.gridspec.SubplotParams` are restored to the default values. (code based on work by @fredrik-1) From 97725781b8193df0f0136876473f23ebfa546554 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Wed, 6 Dec 2023 20:49:30 +0100 Subject: [PATCH 11/36] increase coverage --- lib/matplotlib/tests/test_gridspec.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/matplotlib/tests/test_gridspec.py b/lib/matplotlib/tests/test_gridspec.py index 08d31bcce465..15f7a7372633 100644 --- a/lib/matplotlib/tests/test_gridspec.py +++ b/lib/matplotlib/tests/test_gridspec.py @@ -10,6 +10,16 @@ def test_equal(): assert gs[:, 0] == gs[:, 0] +def test_update(): + gs = gridspec.GridSpec(2, 1) + + gs.update(left=.1) + assert gs.left == .1 + + with pytest.raises(AttributeError): + gs.update(lleft=.1) + + def test_width_ratios(): """ Addresses issue #5835. From 88eed8c81dac9a1918649759a727869ecdbc2ef7 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Tue, 16 Jan 2024 22:40:16 +0100 Subject: [PATCH 12/36] flake8 --- lib/matplotlib/tests/test_figure.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index ad02559abcee..f7fd00b04ca5 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -1748,3 +1748,5 @@ def test_suplots_adjust_plt(): inDict['wspace'] = plt.rcParams['figure.subplot.wspace'] del inDict['rc_default'] assert plt.gcf().subplotpars.get_subplot_params() == inDict + + From da315552bb22ba0df0fbebe7a73c9137a152b079 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Tue, 19 Mar 2024 21:42:58 +0100 Subject: [PATCH 13/36] lint --- lib/matplotlib/tests/test_figure.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index f7fd00b04ca5..ad02559abcee 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -1748,5 +1748,3 @@ def test_suplots_adjust_plt(): inDict['wspace'] = plt.rcParams['figure.subplot.wspace'] del inDict['rc_default'] assert plt.gcf().subplotpars.get_subplot_params() == inDict - - From e39db47c3d405554e49f00823cf64bd212ef3f0f Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Thu, 4 Jul 2024 11:05:08 +0200 Subject: [PATCH 14/36] lint --- lib/matplotlib/tests/test_figure.py | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index b159f32657a7..7a58301aa2bf 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -1807,4 +1807,3 @@ def test_subfigure_stale_propagation(): sfig2.stale = True assert fig.stale - From 0086dcfcf08bc712e94d321bdd4c242f8f6c05e1 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Fri, 6 Dec 2024 19:20:59 +0100 Subject: [PATCH 15/36] Update lib/matplotlib/tests/test_figure.py --- lib/matplotlib/tests/test_figure.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index e99f953452ff..65bcedbb5b97 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -1811,6 +1811,7 @@ def test_suplots_adjust_plt(): del inDict['rc_default'] assert plt.gcf().subplotpars.get_subplot_params() == inDict + def test_set_figure(): fig = plt.figure() sfig1 = fig.subfigures() From e0b75eca968b4d80d79056f1a33486106f546861 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Mon, 14 Apr 2025 17:18:14 +0200 Subject: [PATCH 16/36] increase coverage --- lib/matplotlib/tests/test_gridspec.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/matplotlib/tests/test_gridspec.py b/lib/matplotlib/tests/test_gridspec.py index 15f7a7372633..da986ba42dc0 100644 --- a/lib/matplotlib/tests/test_gridspec.py +++ b/lib/matplotlib/tests/test_gridspec.py @@ -42,10 +42,17 @@ def test_SubplotParams(): s = gridspec.SubplotParams(.1, .1, .9, .9) assert s.left == 0.1 + with pytest.raises(ValueError): + s.update(left=s.right + .01) + + with pytest.raises(ValueError): + s.update(bottom=s.top + .01) + with pytest.raises(ValueError): s = gridspec.SubplotParams(.1, .1, .09, .9) + def test_repr(): ss = gridspec.GridSpec(3, 3)[2, 1:3] assert repr(ss) == "GridSpec(3, 3)[2:3, 1:3]" From ddcc0b4edf5a9eae078a131fad1db00950fa61fc Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Mon, 14 Apr 2025 17:18:30 +0200 Subject: [PATCH 17/36] increase coverage --- lib/matplotlib/tests/test_gridspec.py | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/matplotlib/tests/test_gridspec.py b/lib/matplotlib/tests/test_gridspec.py index da986ba42dc0..d907d8022b0c 100644 --- a/lib/matplotlib/tests/test_gridspec.py +++ b/lib/matplotlib/tests/test_gridspec.py @@ -52,7 +52,6 @@ def test_SubplotParams(): s = gridspec.SubplotParams(.1, .1, .09, .9) - def test_repr(): ss = gridspec.GridSpec(3, 3)[2, 1:3] assert repr(ss) == "GridSpec(3, 3)[2:3, 1:3]" From aa740f39e5c6430b8c198f9de8576fe7f3b53f49 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Fri, 18 Apr 2025 16:29:16 +0200 Subject: [PATCH 18/36] remove usage of rc_default --- lib/matplotlib/figure.py | 11 +++-------- lib/matplotlib/figure.pyi | 1 - lib/matplotlib/gridspec.py | 14 +++++++------- lib/matplotlib/gridspec.pyi | 1 - lib/matplotlib/pyplot.py | 9 +-------- lib/matplotlib/tests/test_figure.py | 8 ++------ lib/matplotlib/tests/test_gridspec.py | 3 +++ 7 files changed, 16 insertions(+), 31 deletions(-) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index e8066e428363..bf4e2253324f 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -989,7 +989,7 @@ def clear(self, keep_observers=False): self.texts = [] self.images = [] self.legends = [] - self.subplotpars.update(rc_default=True) + self.subplotpars.reset() if not keep_observers: self._axobservers = cbook.CallbackRegistry() self._suptitle = None @@ -1312,7 +1312,7 @@ def colorbar( return cb def subplots_adjust(self, left=None, bottom=None, right=None, top=None, - wspace=None, hspace=None, rc_default=False): + wspace=None, hspace=None): """ Adjust the subplot layout parameters. @@ -1341,10 +1341,6 @@ def subplots_adjust(self, left=None, bottom=None, right=None, top=None, hspace : float, optional The height of the padding between subplots, as a fraction of the average Axes height. - rc_default : bool - Determine the defaults. *False*, the default, and the values - are unchanged. *True* and the values are taken from - :rc:`figure.subplot.*` """ if (self.get_layout_engine() is not None and not self.get_layout_engine().adjust_compatible): @@ -1353,8 +1349,7 @@ def subplots_adjust(self, left=None, bottom=None, right=None, top=None, "incompatible with subplots_adjust and/or tight_layout; " "not calling subplots_adjust.") return - self.subplotpars.update(left, bottom, right, top, wspace, hspace, - rc_default=rc_default) + self.subplotpars.update(left, bottom, right, top, wspace, hspace) for ax in self.axes: if ax.get_subplotspec() is not None: ax._set_position(ax.get_subplotspec().get_position(self)) diff --git a/lib/matplotlib/figure.pyi b/lib/matplotlib/figure.pyi index c99e419cb33d..c048ad556036 100644 --- a/lib/matplotlib/figure.pyi +++ b/lib/matplotlib/figure.pyi @@ -179,7 +179,6 @@ class FigureBase(Artist): top: float | None = ..., wspace: float | None = ..., hspace: float | None = ..., - rc_default: bool = ..., ) -> None: ... def align_xlabels(self, axs: Iterable[Axes] | None = ...) -> None: ... def align_ylabels(self, axs: Iterable[Axes] | None = ...) -> None: ... diff --git a/lib/matplotlib/gridspec.py b/lib/matplotlib/gridspec.py index 5a999d9ed202..b27867c0336c 100644 --- a/lib/matplotlib/gridspec.py +++ b/lib/matplotlib/gridspec.py @@ -771,11 +771,9 @@ def _repr_pretty_(self, p: Any, cycle: bool) -> None: p.text(s) def update(self, left=None, bottom=None, right=None, top=None, - wspace=None, hspace=None, rc_default=False): + wspace=None, hspace=None): """ Update the dimensions of the passed parameters. *None* means unchanged. - If *rc_default* is True, then restore the parameters with value *None* - to the default. """ if ((left if left is not None else self.left) >= (right if right is not None else self.right)): @@ -787,12 +785,14 @@ def update(self, left=None, bottom=None, right=None, top=None, attributes = {'left': left, 'right': right, 'bottom': bottom, 'top': top, 'hspace': hspace, 'wspace': wspace} for key, value in attributes.items(): - if value is None: - if rc_default: - setattr(self, key, mpl.rcParams[f'figure.subplot.{key}']) - else: + if value is not None: setattr(self, key, value) + def reset(self): + """ Restore the positioning parameters to the default values """ + for key in self.get_subplot_params.keys(): + setattr(self, key, mpl.rcParams[f'figure.subplot.{key}']) + def get_subplot_params(self) -> dict[str, float]: """ Returns diff --git a/lib/matplotlib/gridspec.pyi b/lib/matplotlib/gridspec.pyi index 8abb22870363..76f9d29c6b1c 100644 --- a/lib/matplotlib/gridspec.pyi +++ b/lib/matplotlib/gridspec.pyi @@ -157,7 +157,6 @@ class SubplotParams: top: float | None = ..., wspace: float | None = ..., hspace: float | None = ..., - rc_default: bool = ..., ) -> None: ... def get_subplot_params( self, diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index d05a84dbc7f3..999b8e42120e 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -2826,16 +2826,9 @@ def subplots_adjust( top: float | None = None, wspace: float | None = None, hspace: float | None = None, - rc_default: bool = False, ) -> None: gcf().subplots_adjust( - left=left, - bottom=bottom, - right=right, - top=top, - wspace=wspace, - hspace=hspace, - rc_default=rc_default, + left=left, bottom=bottom, right=right, top=top, wspace=wspace, hspace=hspace ) diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index 2b49bc9662eb..62f385fb1284 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -1793,22 +1793,18 @@ def test_suplots_adjust_1(): def test_suplots_adjust_2(): fig = plt.figure(1) fig.subplots_adjust(wspace=0) - inDict = dict(left=0.1, right=0.7, bottom=0, top=0.9, hspace=0.05, - rc_default=True) + inDict = dict(left=0.1, right=0.7, bottom=0, top=0.9, hspace=0.05) fig.subplots_adjust(**inDict) inDict['wspace'] = plt.rcParams['figure.subplot.wspace'] - del inDict['rc_default'] assert fig.subplotpars.get_subplot_params() == inDict def test_suplots_adjust_plt(): plt.figure(1) plt.subplots_adjust(wspace=0) - inDict = dict(left=0.1, right=0.7, bottom=0, top=0.9, hspace=0.05, - rc_default=True) + inDict = dict(left=0.1, right=0.7, bottom=0, top=0.9, hspace=0.05) plt.subplots_adjust(**inDict) inDict['wspace'] = plt.rcParams['figure.subplot.wspace'] - del inDict['rc_default'] assert plt.gcf().subplotpars.get_subplot_params() == inDict diff --git a/lib/matplotlib/tests/test_gridspec.py b/lib/matplotlib/tests/test_gridspec.py index d907d8022b0c..9587f906909c 100644 --- a/lib/matplotlib/tests/test_gridspec.py +++ b/lib/matplotlib/tests/test_gridspec.py @@ -1,4 +1,5 @@ import unittest +import matplotlib import matplotlib.gridspec as gridspec import matplotlib.pyplot as plt import pytest @@ -51,6 +52,8 @@ def test_SubplotParams(): with pytest.raises(ValueError): s = gridspec.SubplotParams(.1, .1, .09, .9) + s.reset() + assert s.left == matplotlib.rcParams['figure.subplot.left'] def test_repr(): ss = gridspec.GridSpec(3, 3)[2, 1:3] From 0766ec041c475a829357e44eb14f1b1e814b4fff Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Fri, 18 Apr 2025 16:32:17 +0200 Subject: [PATCH 19/36] update whatsnew --- doc/users/next_whats_new/subplots_adjust.rst | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/doc/users/next_whats_new/subplots_adjust.rst b/doc/users/next_whats_new/subplots_adjust.rst index 5a243eb8ccf7..ab1c0bd2c36f 100644 --- a/doc/users/next_whats_new/subplots_adjust.rst +++ b/doc/users/next_whats_new/subplots_adjust.rst @@ -1,12 +1,10 @@ -subplots_adjust has a new ``kwarg``: ``rc_default`` +Resetting the subplot parameters for figure.clear() --------------------------------------------------- -`.Figure.subplots_adjust` and `.pyplot.subplots_adjust` have a new ``kwarg``: -``rc_default`` that determines the default values for the subplot parameters. +When calling `.Figure.clear()` the settings for `.gridspec.SubplotParams` are restored to the default values. -The `.gridspec.SubplotParams` object has a new get method -:meth:`~.SubplotParams.get_subplot_params` +The `.gridspec.SubplotParams` object has a new get method :meth:`~.SubplotParams.get_subplot_params` and a +method to reset the parameters to the defaults :meth:`~.SubplotParams.reset` -When calling `.Figure.clear()` the settings for `.gridspec.SubplotParams` are restored to the default values. -(code based on work by @fredrik-1) +(contributed by @eendebakpt based on work by @fredrik-1) From 50e9b46b90de0fd29a3025230c2a922985097607 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Fri, 18 Apr 2025 16:33:27 +0200 Subject: [PATCH 20/36] whitespace --- lib/matplotlib/gridspec.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/gridspec.py b/lib/matplotlib/gridspec.py index b27867c0336c..357f5f383322 100644 --- a/lib/matplotlib/gridspec.py +++ b/lib/matplotlib/gridspec.py @@ -789,7 +789,7 @@ def update(self, left=None, bottom=None, right=None, top=None, setattr(self, key, value) def reset(self): - """ Restore the positioning parameters to the default values """ + """Restore the positioning parameters to the default values""" for key in self.get_subplot_params.keys(): setattr(self, key, mpl.rcParams[f'figure.subplot.{key}']) From 3690d94418bcc10628eb76b18d7d9c51b5aa54d2 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Fri, 18 Apr 2025 16:37:26 +0200 Subject: [PATCH 21/36] whitespace --- lib/matplotlib/gridspec.py | 2 +- lib/matplotlib/gridspec.pyi | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/gridspec.py b/lib/matplotlib/gridspec.py index 357f5f383322..1af4fa0fd272 100644 --- a/lib/matplotlib/gridspec.py +++ b/lib/matplotlib/gridspec.py @@ -790,7 +790,7 @@ def update(self, left=None, bottom=None, right=None, top=None, def reset(self): """Restore the positioning parameters to the default values""" - for key in self.get_subplot_params.keys(): + for key in self.get_subplot_params().keys(): setattr(self, key, mpl.rcParams[f'figure.subplot.{key}']) def get_subplot_params(self) -> dict[str, float]: diff --git a/lib/matplotlib/gridspec.pyi b/lib/matplotlib/gridspec.pyi index 76f9d29c6b1c..54357c3d093b 100644 --- a/lib/matplotlib/gridspec.pyi +++ b/lib/matplotlib/gridspec.pyi @@ -161,3 +161,4 @@ class SubplotParams: def get_subplot_params( self, ) -> dict[str, float]: ... + def reset(self) -> None: ... From e119ab0884133917e1c590017bbcc126c9d4cc38 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Mon, 21 Apr 2025 21:42:56 +0200 Subject: [PATCH 22/36] Apply suggestions from code review Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> --- lib/matplotlib/gridspec.py | 10 ++-------- lib/matplotlib/gridspec.pyi | 2 +- lib/matplotlib/tests/test_figure.py | 16 +++++++--------- lib/matplotlib/tests/test_gridspec.py | 4 ++-- 4 files changed, 12 insertions(+), 20 deletions(-) diff --git a/lib/matplotlib/gridspec.py b/lib/matplotlib/gridspec.py index 1af4fa0fd272..23c775df49de 100644 --- a/lib/matplotlib/gridspec.py +++ b/lib/matplotlib/gridspec.py @@ -9,7 +9,6 @@ methods like `~.pyplot.subplots`, `~.pyplot.subplot_mosaic` and `~.Figure.subfigures`. See the tutorial :ref:`arranging_axes` for a guide. """ -from typing import Any import copy import logging from numbers import Integral @@ -793,11 +792,6 @@ def reset(self): for key in self.get_subplot_params().keys(): setattr(self, key, mpl.rcParams[f'figure.subplot.{key}']) - def get_subplot_params(self) -> dict[str, float]: - """ - Returns - ------- - subplot_params : dictionary - A dictionary with the subplot parameters - """ + def to_dict(self): + """Return the subplot parameters as a dict.""" return self.__dict__.copy() diff --git a/lib/matplotlib/gridspec.pyi b/lib/matplotlib/gridspec.pyi index 54357c3d093b..04de229f4d42 100644 --- a/lib/matplotlib/gridspec.pyi +++ b/lib/matplotlib/gridspec.pyi @@ -158,7 +158,7 @@ class SubplotParams: wspace: float | None = ..., hspace: float | None = ..., ) -> None: ... - def get_subplot_params( + def to_dict( self, ) -> dict[str, float]: ... def reset(self) -> None: ... diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index 62f385fb1284..eb6ee52e65d1 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -1775,19 +1775,17 @@ def test_clf_subplotpars(): rc_params = {key: plt.rcParams['figure.subplot.'+key] for key in keys} fig = plt.figure(1) - fig.subplots_adjust(left=0.1) + fig.subplots_adjust({k, v+0.01 for k, v in rc_params.items()) fig.clf() assert fig.subplotpars.get_subplot_params() == rc_params -def test_suplots_adjust_1(): - fig = plt.figure(1) - wspace = 0 - fig.subplots_adjust(wspace=wspace) - inDict = dict(left=0.1, right=0.7, bottom=0, top=0.9, hspace=0.05) - fig.subplots_adjust(**inDict) - inDict['wspace'] = wspace - assert fig.subplotpars.get_subplot_params() == inDict +def test_suplots_adjust_incremental(): + fig = plt.figure() + fig.subplots_adjust(left=0) + fig.subplots_adjust(right=1) + assert fig.subplotpars.left == 0 + assert fig.subplotpars.right == 1 def test_suplots_adjust_2(): diff --git a/lib/matplotlib/tests/test_gridspec.py b/lib/matplotlib/tests/test_gridspec.py index 9587f906909c..7b86d44c1777 100644 --- a/lib/matplotlib/tests/test_gridspec.py +++ b/lib/matplotlib/tests/test_gridspec.py @@ -43,10 +43,10 @@ def test_SubplotParams(): s = gridspec.SubplotParams(.1, .1, .9, .9) assert s.left == 0.1 - with pytest.raises(ValueError): + with pytest.raises(ValueError, match='left cannot be >= right'): s.update(left=s.right + .01) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match='bottom cannot be >= top'): s.update(bottom=s.top + .01) with pytest.raises(ValueError): From 275825fa267c7b9b68a168647af4696328779962 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Mon, 21 Apr 2025 21:56:06 +0200 Subject: [PATCH 23/36] review comments --- doc/users/next_whats_new/subplots_adjust.rst | 3 +-- lib/matplotlib/gridspec.py | 25 ++++++++++---------- lib/matplotlib/tests/test_gridspec.py | 11 +++------ 3 files changed, 16 insertions(+), 23 deletions(-) diff --git a/doc/users/next_whats_new/subplots_adjust.rst b/doc/users/next_whats_new/subplots_adjust.rst index ab1c0bd2c36f..94399f869aab 100644 --- a/doc/users/next_whats_new/subplots_adjust.rst +++ b/doc/users/next_whats_new/subplots_adjust.rst @@ -3,8 +3,7 @@ Resetting the subplot parameters for figure.clear() When calling `.Figure.clear()` the settings for `.gridspec.SubplotParams` are restored to the default values. -The `.gridspec.SubplotParams` object has a new get method :meth:`~.SubplotParams.get_subplot_params` and a +The `.gridspec.SubplotParams` object has a new method to get the subplot parameters :meth:`~.SubplotParams.to_dict` and a method to reset the parameters to the defaults :meth:`~.SubplotParams.reset` - (contributed by @eendebakpt based on work by @fredrik-1) diff --git a/lib/matplotlib/gridspec.py b/lib/matplotlib/gridspec.py index 1af4fa0fd272..d82035c1f37e 100644 --- a/lib/matplotlib/gridspec.py +++ b/lib/matplotlib/gridspec.py @@ -9,7 +9,6 @@ methods like `~.pyplot.subplots`, `~.pyplot.subplot_mosaic` and `~.Figure.subfigures`. See the tutorial :ref:`arranging_axes` for a guide. """ -from typing import Any import copy import logging from numbers import Integral @@ -763,13 +762,6 @@ def __init__(self, left=None, bottom=None, right=None, top=None, setattr(self, key, mpl.rcParams[f"figure.subplot.{key}"]) self.update(left, bottom, right, top, wspace, hspace) - def _repr_pretty_(self, p: Any, cycle: bool) -> None: - del cycle - name = self.__class__.__name__ - s = f"{name}(left={self.left}, bottom={self.bottom}, right={self.right}, " - s += f" top={self.top}, wspace={self.wspace}, hspace={self.hspace})" - p.text(s) - def update(self, left=None, bottom=None, right=None, top=None, wspace=None, hspace=None): """ @@ -782,11 +774,18 @@ def update(self, left=None, bottom=None, right=None, top=None, >= (top if top is not None else self.top)): raise ValueError('bottom cannot be >= top') - attributes = {'left': left, 'right': right, 'bottom': bottom, 'top': top, - 'hspace': hspace, 'wspace': wspace} - for key, value in attributes.items(): - if value is not None: - setattr(self, key, value) + if left is not None: + self.left = left + if right is not None: + self.right = right + if bottom is not None: + self.bottom = bottom + if top is not None: + self.top = top + if wspace is not None: + self.wspace = wspace + if hspace is not None: + self.hspace = hspace def reset(self): """Restore the positioning parameters to the default values""" diff --git a/lib/matplotlib/tests/test_gridspec.py b/lib/matplotlib/tests/test_gridspec.py index 9587f906909c..6fe7eb06bba8 100644 --- a/lib/matplotlib/tests/test_gridspec.py +++ b/lib/matplotlib/tests/test_gridspec.py @@ -1,4 +1,3 @@ -import unittest import matplotlib import matplotlib.gridspec as gridspec import matplotlib.pyplot as plt @@ -43,6 +42,9 @@ def test_SubplotParams(): s = gridspec.SubplotParams(.1, .1, .9, .9) assert s.left == 0.1 + s.reset() + assert s.left == matplotlib.rcParams['figure.subplot.left'] + with pytest.raises(ValueError): s.update(left=s.right + .01) @@ -52,8 +54,6 @@ def test_SubplotParams(): with pytest.raises(ValueError): s = gridspec.SubplotParams(.1, .1, .09, .9) - s.reset() - assert s.left == matplotlib.rcParams['figure.subplot.left'] def test_repr(): ss = gridspec.GridSpec(3, 3)[2, 1:3] @@ -76,8 +76,3 @@ def test_subplotspec_args(): gs = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=axs[0]) with pytest.raises(TypeError, match="subplot_spec must be type SubplotSpec"): gs = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=axs) - - s = gridspec.SubplotParams(.1, .1, .9, .9) - p = unittest.mock.MagicMock() - s._repr_pretty_(p, False) - assert 'SubplotParams' in p.method_calls[0].args[0] From 7665053df0efac42a39fed5b78f9602b9a050db8 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Mon, 21 Apr 2025 22:06:20 +0200 Subject: [PATCH 24/36] fix merge conflicts --- lib/matplotlib/gridspec.py | 3 ++- lib/matplotlib/tests/test_figure.py | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/gridspec.py b/lib/matplotlib/gridspec.py index e2ffdb9dec35..2163b823f65d 100644 --- a/lib/matplotlib/gridspec.py +++ b/lib/matplotlib/gridspec.py @@ -9,6 +9,7 @@ methods like `~.pyplot.subplots`, `~.pyplot.subplot_mosaic` and `~.Figure.subfigures`. See the tutorial :ref:`arranging_axes` for a guide. """ + import copy import logging from numbers import Integral @@ -789,7 +790,7 @@ def update(self, left=None, bottom=None, right=None, top=None, def reset(self): """Restore the positioning parameters to the default values""" - for key in self.get_subplot_params().keys(): + for key in self.to_dict(): setattr(self, key, mpl.rcParams[f'figure.subplot.{key}']) def to_dict(self): diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index eb6ee52e65d1..4e0e88793fb5 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -1775,7 +1775,7 @@ def test_clf_subplotpars(): rc_params = {key: plt.rcParams['figure.subplot.'+key] for key in keys} fig = plt.figure(1) - fig.subplots_adjust({k, v+0.01 for k, v in rc_params.items()) + fig.subplots_adjust({k: v+0.01 for k, v in rc_params.items()}) fig.clf() assert fig.subplotpars.get_subplot_params() == rc_params @@ -1794,7 +1794,7 @@ def test_suplots_adjust_2(): inDict = dict(left=0.1, right=0.7, bottom=0, top=0.9, hspace=0.05) fig.subplots_adjust(**inDict) inDict['wspace'] = plt.rcParams['figure.subplot.wspace'] - assert fig.subplotpars.get_subplot_params() == inDict + assert fig.subplotpars.to_dict() == inDict def test_suplots_adjust_plt(): @@ -1803,7 +1803,7 @@ def test_suplots_adjust_plt(): inDict = dict(left=0.1, right=0.7, bottom=0, top=0.9, hspace=0.05) plt.subplots_adjust(**inDict) inDict['wspace'] = plt.rcParams['figure.subplot.wspace'] - assert plt.gcf().subplotpars.get_subplot_params() == inDict + assert plt.gcf().subplotpars.to_dict() == inDict def test_set_figure(): From 10d8216f69d6af89e4195a8d757cee3c65d46c20 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Mon, 21 Apr 2025 22:07:57 +0200 Subject: [PATCH 25/36] remove duplicate test --- lib/matplotlib/tests/test_figure.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index 4e0e88793fb5..c9317ecec83c 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -1793,19 +1793,9 @@ def test_suplots_adjust_2(): fig.subplots_adjust(wspace=0) inDict = dict(left=0.1, right=0.7, bottom=0, top=0.9, hspace=0.05) fig.subplots_adjust(**inDict) - inDict['wspace'] = plt.rcParams['figure.subplot.wspace'] assert fig.subplotpars.to_dict() == inDict -def test_suplots_adjust_plt(): - plt.figure(1) - plt.subplots_adjust(wspace=0) - inDict = dict(left=0.1, right=0.7, bottom=0, top=0.9, hspace=0.05) - plt.subplots_adjust(**inDict) - inDict['wspace'] = plt.rcParams['figure.subplot.wspace'] - assert plt.gcf().subplotpars.to_dict() == inDict - - def test_set_figure(): fig = plt.figure() sfig1 = fig.subfigures() From d3f8db177e800b327f7b7f7d1c4562dd7b3d3e0e Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Mon, 21 Apr 2025 22:16:55 +0200 Subject: [PATCH 26/36] remove redundant test --- lib/matplotlib/gridspec.py | 1 - lib/matplotlib/tests/test_figure.py | 13 ++----------- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/lib/matplotlib/gridspec.py b/lib/matplotlib/gridspec.py index 2163b823f65d..b753301d9280 100644 --- a/lib/matplotlib/gridspec.py +++ b/lib/matplotlib/gridspec.py @@ -774,7 +774,6 @@ def update(self, left=None, bottom=None, right=None, top=None, if ((bottom if bottom is not None else self.bottom) >= (top if top is not None else self.top)): raise ValueError('bottom cannot be >= top') - if left is not None: self.left = left if right is not None: diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index c9317ecec83c..fb048c010784 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -1772,12 +1772,12 @@ def test_warn_colorbar_mismatch(): def test_clf_subplotpars(): keys = ('left', 'right', 'bottom', 'top', 'wspace', 'hspace') - rc_params = {key: plt.rcParams['figure.subplot.'+key] for key in keys} + rc_params = {key: plt.rcParams['figure.subplot.' + key] for key in keys} fig = plt.figure(1) fig.subplots_adjust({k: v+0.01 for k, v in rc_params.items()}) fig.clf() - assert fig.subplotpars.get_subplot_params() == rc_params + assert fig.subplotpars.to_dict() == rc_params def test_suplots_adjust_incremental(): @@ -1787,15 +1787,6 @@ def test_suplots_adjust_incremental(): assert fig.subplotpars.left == 0 assert fig.subplotpars.right == 1 - -def test_suplots_adjust_2(): - fig = plt.figure(1) - fig.subplots_adjust(wspace=0) - inDict = dict(left=0.1, right=0.7, bottom=0, top=0.9, hspace=0.05) - fig.subplots_adjust(**inDict) - assert fig.subplotpars.to_dict() == inDict - - def test_set_figure(): fig = plt.figure() sfig1 = fig.subfigures() From 6725328b12d9912d28c6e11e2f64357e8f77527d Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Mon, 21 Apr 2025 22:29:42 +0200 Subject: [PATCH 27/36] ci --- lib/matplotlib/tests/test_figure.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index fb048c010784..32ef8f95f9d1 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -1775,7 +1775,7 @@ def test_clf_subplotpars(): rc_params = {key: plt.rcParams['figure.subplot.' + key] for key in keys} fig = plt.figure(1) - fig.subplots_adjust({k: v+0.01 for k, v in rc_params.items()}) + fig.subplots_adjust(**{k: v+0.01 for k, v in rc_params.items()}) fig.clf() assert fig.subplotpars.to_dict() == rc_params From a91275e73ce56ce75cf7a084a6955927efcd8255 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Mon, 21 Apr 2025 22:38:02 +0200 Subject: [PATCH 28/36] ci --- lib/matplotlib/tests/test_figure.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index 32ef8f95f9d1..496ac0a071ec 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -1787,6 +1787,7 @@ def test_suplots_adjust_incremental(): assert fig.subplotpars.left == 0 assert fig.subplotpars.right == 1 + def test_set_figure(): fig = plt.figure() sfig1 = fig.subfigures() From 7d3e38626a97cfd3b6731efa71f790f0fdf72f7d Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Tue, 22 Apr 2025 10:42:42 +0200 Subject: [PATCH 29/36] Update lib/matplotlib/tests/test_gridspec.py Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> --- lib/matplotlib/tests/test_gridspec.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_gridspec.py b/lib/matplotlib/tests/test_gridspec.py index 1e2462939342..82551e1215de 100644 --- a/lib/matplotlib/tests/test_gridspec.py +++ b/lib/matplotlib/tests/test_gridspec.py @@ -51,8 +51,8 @@ def test_SubplotParams(): with pytest.raises(ValueError, match='bottom cannot be >= top'): s.update(bottom=s.top + .01) - with pytest.raises(ValueError): - s = gridspec.SubplotParams(.1, .1, .09, .9) + with pytest.raises(ValueError, match='left cannot be >= right'): + gridspec.SubplotParams(.1, .1, .09, .9) def test_repr(): From 9d772c465bf8bb0d1e60ed67ef6a96cc3e84e34e Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Tue, 22 Apr 2025 10:43:03 +0200 Subject: [PATCH 30/36] Update lib/matplotlib/tests/test_gridspec.py Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> --- lib/matplotlib/tests/test_gridspec.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/matplotlib/tests/test_gridspec.py b/lib/matplotlib/tests/test_gridspec.py index 82551e1215de..625a79816ed3 100644 --- a/lib/matplotlib/tests/test_gridspec.py +++ b/lib/matplotlib/tests/test_gridspec.py @@ -16,9 +16,6 @@ def test_update(): gs.update(left=.1) assert gs.left == .1 - with pytest.raises(AttributeError): - gs.update(lleft=.1) - def test_width_ratios(): """ From 98df0da383d49ca59cf1a2709cc28c1156133429 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Tue, 22 Apr 2025 20:22:33 +0200 Subject: [PATCH 31/36] Update doc/users/next_whats_new/subplots_adjust.rst Co-authored-by: Jody Klymak --- doc/users/next_whats_new/subplots_adjust.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/users/next_whats_new/subplots_adjust.rst b/doc/users/next_whats_new/subplots_adjust.rst index 94399f869aab..d68758244fc5 100644 --- a/doc/users/next_whats_new/subplots_adjust.rst +++ b/doc/users/next_whats_new/subplots_adjust.rst @@ -6,4 +6,3 @@ When calling `.Figure.clear()` the settings for `.gridspec.SubplotParams` are re The `.gridspec.SubplotParams` object has a new method to get the subplot parameters :meth:`~.SubplotParams.to_dict` and a method to reset the parameters to the defaults :meth:`~.SubplotParams.reset` -(contributed by @eendebakpt based on work by @fredrik-1) From fac13d12268c711381e53eb350781eda6ed17dfd Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Tue, 22 Apr 2025 20:22:56 +0200 Subject: [PATCH 32/36] Update lib/matplotlib/gridspec.py Co-authored-by: Jody Klymak --- lib/matplotlib/gridspec.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/gridspec.py b/lib/matplotlib/gridspec.py index b753301d9280..04fd85333202 100644 --- a/lib/matplotlib/gridspec.py +++ b/lib/matplotlib/gridspec.py @@ -788,7 +788,7 @@ def update(self, left=None, bottom=None, right=None, top=None, self.hspace = hspace def reset(self): - """Restore the positioning parameters to the default values""" + """Restore the subplot positioning parameters to the default rcParams values""" for key in self.to_dict(): setattr(self, key, mpl.rcParams[f'figure.subplot.{key}']) From 49d1ecad7cbce4dc6b24325e35f5366202959c7f Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Tue, 22 Apr 2025 20:23:07 +0200 Subject: [PATCH 33/36] Update lib/matplotlib/gridspec.py Co-authored-by: Jody Klymak --- lib/matplotlib/gridspec.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/gridspec.py b/lib/matplotlib/gridspec.py index 04fd85333202..3b85cd65d10d 100644 --- a/lib/matplotlib/gridspec.py +++ b/lib/matplotlib/gridspec.py @@ -793,5 +793,5 @@ def reset(self): setattr(self, key, mpl.rcParams[f'figure.subplot.{key}']) def to_dict(self): - """Return the subplot parameters as a dict.""" + """Return a copy of the subplot parameters as a dict.""" return self.__dict__.copy() From 5c9674a5047a441edd126998ee56db9dac7e8a59 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Tue, 22 Apr 2025 22:42:53 +0200 Subject: [PATCH 34/36] whitespace --- doc/users/next_whats_new/subplots_adjust.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/users/next_whats_new/subplots_adjust.rst b/doc/users/next_whats_new/subplots_adjust.rst index d68758244fc5..5eb33f7a0186 100644 --- a/doc/users/next_whats_new/subplots_adjust.rst +++ b/doc/users/next_whats_new/subplots_adjust.rst @@ -5,4 +5,3 @@ When calling `.Figure.clear()` the settings for `.gridspec.SubplotParams` are re The `.gridspec.SubplotParams` object has a new method to get the subplot parameters :meth:`~.SubplotParams.to_dict` and a method to reset the parameters to the defaults :meth:`~.SubplotParams.reset` - From 5ad2d55f7a328796f809ffb8ae560eab6c5ac123 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Wed, 23 Apr 2025 06:21:02 +0200 Subject: [PATCH 35/36] Update doc/users/next_whats_new/subplots_adjust.rst Co-authored-by: Jody Klymak --- doc/users/next_whats_new/subplots_adjust.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/users/next_whats_new/subplots_adjust.rst b/doc/users/next_whats_new/subplots_adjust.rst index 5eb33f7a0186..da4864fa12bb 100644 --- a/doc/users/next_whats_new/subplots_adjust.rst +++ b/doc/users/next_whats_new/subplots_adjust.rst @@ -3,5 +3,5 @@ Resetting the subplot parameters for figure.clear() When calling `.Figure.clear()` the settings for `.gridspec.SubplotParams` are restored to the default values. -The `.gridspec.SubplotParams` object has a new method to get the subplot parameters :meth:`~.SubplotParams.to_dict` and a -method to reset the parameters to the defaults :meth:`~.SubplotParams.reset` +`~.SubplotParams.to_dict` is a new method to get the subplot parameters as a dict, +and `~.SubplotParams.reset` resets the parameters to the defaults. From d5c9f7b7ea1886d7b761581becb1fb873ca4a7a1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 23 Apr 2025 06:23:17 +0000 Subject: [PATCH 36/36] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- doc/users/next_whats_new/subplots_adjust.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/users/next_whats_new/subplots_adjust.rst b/doc/users/next_whats_new/subplots_adjust.rst index da4864fa12bb..e0848ec8a3dc 100644 --- a/doc/users/next_whats_new/subplots_adjust.rst +++ b/doc/users/next_whats_new/subplots_adjust.rst @@ -3,5 +3,5 @@ Resetting the subplot parameters for figure.clear() When calling `.Figure.clear()` the settings for `.gridspec.SubplotParams` are restored to the default values. -`~.SubplotParams.to_dict` is a new method to get the subplot parameters as a dict, +`~.SubplotParams.to_dict` is a new method to get the subplot parameters as a dict, and `~.SubplotParams.reset` resets the parameters to the defaults.