Skip to content

Commit 00d5cb6

Browse files
eendebakptksundentimhoffmjklymak
authored
Fix behaviour of Figure.clear() for SubplotParams (matplotlib#27183)
* Fix behaviour of Figure.clear() for SubplotParams * update credits * lint * lint * lint * lint * run boilerplate.py * add tests * Update doc/users/next_whats_new/subplots_adjust.rst Co-authored-by: Kyle Sunden <[email protected]> * update news entry * increase coverage * flake8 * lint * lint * Update lib/matplotlib/tests/test_figure.py * increase coverage * increase coverage * remove usage of rc_default * update whatsnew * whitespace * whitespace * Apply suggestions from code review Co-authored-by: Tim Hoffmann <[email protected]> * review comments * fix merge conflicts * remove duplicate test * remove redundant test * ci * ci * Update lib/matplotlib/tests/test_gridspec.py Co-authored-by: Tim Hoffmann <[email protected]> * Update lib/matplotlib/tests/test_gridspec.py Co-authored-by: Tim Hoffmann <[email protected]> * Update doc/users/next_whats_new/subplots_adjust.rst Co-authored-by: Jody Klymak <[email protected]> * Update lib/matplotlib/gridspec.py Co-authored-by: Jody Klymak <[email protected]> * Update lib/matplotlib/gridspec.py Co-authored-by: Jody Klymak <[email protected]> * whitespace * Update doc/users/next_whats_new/subplots_adjust.rst Co-authored-by: Jody Klymak <[email protected]> --------- Co-authored-by: Kyle Sunden <[email protected]> Co-authored-by: Tim Hoffmann <[email protected]> Co-authored-by: Jody Klymak <[email protected]>
1 parent 697c240 commit 00d5cb6

File tree

6 files changed

+70
-6
lines changed

6 files changed

+70
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Resetting the subplot parameters for figure.clear()
2+
---------------------------------------------------
3+
4+
When calling `.Figure.clear()` the settings for `.gridspec.SubplotParams` are restored to the default values.
5+
6+
`~.SubplotParams.to_dict` is a new method to get the subplot parameters as a dict,
7+
and `~.SubplotParams.reset` resets the parameters to the defaults.

lib/matplotlib/figure.py

+1
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,7 @@ def clear(self, keep_observers=False):
989989
self.texts = []
990990
self.images = []
991991
self.legends = []
992+
self.subplotpars.reset()
992993
if not keep_observers:
993994
self._axobservers = cbook.CallbackRegistry()
994995
self._suptitle = None

lib/matplotlib/gridspec.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -750,22 +750,22 @@ def __init__(self, left=None, bottom=None, right=None, top=None,
750750
751751
Parameters
752752
----------
753-
left : float
753+
left : float, optional
754754
The position of the left edge of the subplots,
755755
as a fraction of the figure width.
756-
right : float
756+
right : float, optional
757757
The position of the right edge of the subplots,
758758
as a fraction of the figure width.
759-
bottom : float
759+
bottom : float, optional
760760
The position of the bottom edge of the subplots,
761761
as a fraction of the figure height.
762-
top : float
762+
top : float, optional
763763
The position of the top edge of the subplots,
764764
as a fraction of the figure height.
765-
wspace : float
765+
wspace : float, optional
766766
The width of the padding between subplots,
767767
as a fraction of the average Axes width.
768-
hspace : float
768+
hspace : float, optional
769769
The height of the padding between subplots,
770770
as a fraction of the average Axes height.
771771
"""
@@ -796,3 +796,12 @@ def update(self, left=None, bottom=None, right=None, top=None,
796796
self.wspace = wspace
797797
if hspace is not None:
798798
self.hspace = hspace
799+
800+
def reset(self):
801+
"""Restore the subplot positioning parameters to the default rcParams values"""
802+
for key in self.to_dict():
803+
setattr(self, key, mpl.rcParams[f'figure.subplot.{key}'])
804+
805+
def to_dict(self):
806+
"""Return a copy of the subplot parameters as a dict."""
807+
return self.__dict__.copy()

lib/matplotlib/gridspec.pyi

+4
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,7 @@ class SubplotParams:
168168
wspace: float | None = ...,
169169
hspace: float | None = ...,
170170
) -> None: ...
171+
def to_dict(
172+
self,
173+
) -> dict[str, float]: ...
174+
def reset(self) -> None: ...

lib/matplotlib/tests/test_figure.py

+18
Original file line numberDiff line numberDiff line change
@@ -1770,6 +1770,24 @@ def test_warn_colorbar_mismatch():
17701770
subfig3_1.colorbar(im4_1)
17711771

17721772

1773+
def test_clf_subplotpars():
1774+
keys = ('left', 'right', 'bottom', 'top', 'wspace', 'hspace')
1775+
rc_params = {key: plt.rcParams['figure.subplot.' + key] for key in keys}
1776+
1777+
fig = plt.figure(1)
1778+
fig.subplots_adjust(**{k: v+0.01 for k, v in rc_params.items()})
1779+
fig.clf()
1780+
assert fig.subplotpars.to_dict() == rc_params
1781+
1782+
1783+
def test_suplots_adjust_incremental():
1784+
fig = plt.figure()
1785+
fig.subplots_adjust(left=0)
1786+
fig.subplots_adjust(right=1)
1787+
assert fig.subplotpars.left == 0
1788+
assert fig.subplotpars.right == 1
1789+
1790+
17731791
def test_set_figure():
17741792
fig = plt.figure()
17751793
sfig1 = fig.subfigures()

lib/matplotlib/tests/test_gridspec.py

+25
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import matplotlib
12
import matplotlib.gridspec as gridspec
23
import matplotlib.pyplot as plt
34
import pytest
@@ -9,6 +10,13 @@ def test_equal():
910
assert gs[:, 0] == gs[:, 0]
1011

1112

13+
def test_update():
14+
gs = gridspec.GridSpec(2, 1)
15+
16+
gs.update(left=.1)
17+
assert gs.left == .1
18+
19+
1220
def test_width_ratios():
1321
"""
1422
Addresses issue #5835.
@@ -27,6 +35,23 @@ def test_height_ratios():
2735
gridspec.GridSpec(1, 1, height_ratios=[2, 1, 3])
2836

2937

38+
def test_SubplotParams():
39+
s = gridspec.SubplotParams(.1, .1, .9, .9)
40+
assert s.left == 0.1
41+
42+
s.reset()
43+
assert s.left == matplotlib.rcParams['figure.subplot.left']
44+
45+
with pytest.raises(ValueError, match='left cannot be >= right'):
46+
s.update(left=s.right + .01)
47+
48+
with pytest.raises(ValueError, match='bottom cannot be >= top'):
49+
s.update(bottom=s.top + .01)
50+
51+
with pytest.raises(ValueError, match='left cannot be >= right'):
52+
gridspec.SubplotParams(.1, .1, .09, .9)
53+
54+
3055
def test_repr():
3156
ss = gridspec.GridSpec(3, 3)[2, 1:3]
3257
assert repr(ss) == "GridSpec(3, 3)[2:3, 1:3]"

0 commit comments

Comments
 (0)