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

Skip to content

Fix behaviour of Figure.clear() for SubplotParams #27183

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 43 commits into from
Apr 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
cf6032e
Fix behaviour of Figure.clear() for SubplotParams
eendebakpt Oct 24, 2023
68386fa
update credits
eendebakpt Oct 24, 2023
035a9c0
lint
eendebakpt Oct 24, 2023
c2c13ff
lint
eendebakpt Oct 24, 2023
247833c
lint
eendebakpt Oct 24, 2023
e2039f8
lint
eendebakpt Oct 24, 2023
1b76817
run boilerplate.py
eendebakpt Oct 25, 2023
51876fc
add tests
eendebakpt Oct 25, 2023
1470bf2
Update doc/users/next_whats_new/subplots_adjust.rst
eendebakpt Dec 6, 2023
199cc4a
update news entry
eendebakpt Dec 6, 2023
9772578
increase coverage
eendebakpt Dec 6, 2023
88eed8c
flake8
eendebakpt Jan 16, 2024
da31555
lint
eendebakpt Mar 19, 2024
12f26bd
Merge branch 'main' into clf_subplots_adjust
eendebakpt Jul 4, 2024
e39db47
lint
eendebakpt Jul 4, 2024
03ccff6
Merge branch 'main' into clf_subplots_adjust
eendebakpt Dec 6, 2024
0086dcf
Update lib/matplotlib/tests/test_figure.py
eendebakpt Dec 6, 2024
e9b5a61
Merge branch 'main' into clf_subplots_adjust
eendebakpt Jan 2, 2025
ded306c
Merge branch 'clf_subplots_adjust' of github.com:eendebakpt/matplotli…
eendebakpt Jan 2, 2025
5e40238
Merge branch 'main' into clf_subplots_adjust
eendebakpt Apr 14, 2025
e0b75ec
increase coverage
eendebakpt Apr 14, 2025
ddcc0b4
increase coverage
eendebakpt Apr 14, 2025
aa740f3
remove usage of rc_default
eendebakpt Apr 18, 2025
0766ec0
update whatsnew
eendebakpt Apr 18, 2025
50e9b46
whitespace
eendebakpt Apr 18, 2025
3690d94
whitespace
eendebakpt Apr 18, 2025
e119ab0
Apply suggestions from code review
eendebakpt Apr 21, 2025
275825f
review comments
eendebakpt Apr 21, 2025
b2cf506
Merge branch 'clf_subplots_adjust' of github.com:eendebakpt/matplotli…
eendebakpt Apr 21, 2025
7665053
fix merge conflicts
eendebakpt Apr 21, 2025
10d8216
remove duplicate test
eendebakpt Apr 21, 2025
d3f8db1
remove redundant test
eendebakpt Apr 21, 2025
6725328
ci
eendebakpt Apr 21, 2025
a91275e
ci
eendebakpt Apr 21, 2025
7d3e386
Update lib/matplotlib/tests/test_gridspec.py
eendebakpt Apr 22, 2025
9d772c4
Update lib/matplotlib/tests/test_gridspec.py
eendebakpt Apr 22, 2025
98df0da
Update doc/users/next_whats_new/subplots_adjust.rst
eendebakpt Apr 22, 2025
fac13d1
Update lib/matplotlib/gridspec.py
eendebakpt Apr 22, 2025
49d1eca
Update lib/matplotlib/gridspec.py
eendebakpt Apr 22, 2025
5c9674a
whitespace
eendebakpt Apr 22, 2025
78d699c
Merge branch 'main' into clf_subplots_adjust
eendebakpt Apr 22, 2025
5ad2d55
Update doc/users/next_whats_new/subplots_adjust.rst
timhoffm Apr 23, 2025
d5c9f7b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 23, 2025
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
7 changes: 7 additions & 0 deletions doc/users/next_whats_new/subplots_adjust.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
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,
and `~.SubplotParams.reset` resets the parameters to the defaults.
1 change: 1 addition & 0 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,7 @@ def clear(self, keep_observers=False):
self.texts = []
self.images = []
self.legends = []
self.subplotpars.reset()
if not keep_observers:
self._axobservers = cbook.CallbackRegistry()
self._suptitle = None
Expand Down
21 changes: 15 additions & 6 deletions lib/matplotlib/gridspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,22 +750,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.
"""
Expand Down Expand Up @@ -796,3 +796,12 @@ def update(self, left=None, bottom=None, right=None, top=None,
self.wspace = wspace
if hspace is not None:
self.hspace = hspace

def reset(self):
"""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}'])

def to_dict(self):
"""Return a copy of the subplot parameters as a dict."""
return self.__dict__.copy()
4 changes: 4 additions & 0 deletions lib/matplotlib/gridspec.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,7 @@ class SubplotParams:
wspace: float | None = ...,
hspace: float | None = ...,
) -> None: ...
def to_dict(
self,
) -> dict[str, float]: ...
def reset(self) -> None: ...
18 changes: 18 additions & 0 deletions lib/matplotlib/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1770,6 +1770,24 @@ def test_warn_colorbar_mismatch():
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(**{k: v+0.01 for k, v in rc_params.items()})
fig.clf()
assert fig.subplotpars.to_dict() == rc_params


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_set_figure():
fig = plt.figure()
sfig1 = fig.subfigures()
Expand Down
25 changes: 25 additions & 0 deletions lib/matplotlib/tests/test_gridspec.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import matplotlib
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
import pytest
Expand All @@ -9,6 +10,13 @@ def test_equal():
assert gs[:, 0] == gs[:, 0]


def test_update():
gs = gridspec.GridSpec(2, 1)

gs.update(left=.1)
assert gs.left == .1


def test_width_ratios():
"""
Addresses issue #5835.
Expand All @@ -27,6 +35,23 @@ 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

s.reset()
assert s.left == matplotlib.rcParams['figure.subplot.left']

with pytest.raises(ValueError, match='left cannot be >= right'):
s.update(left=s.right + .01)

with pytest.raises(ValueError, match='bottom cannot be >= top'):
s.update(bottom=s.top + .01)

with pytest.raises(ValueError, match='left cannot be >= right'):
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]"
Expand Down
Loading