8000 fix figure.clf and subplot_adjust · matplotlib/matplotlib@de2f714 · GitHub
[go: up one dir, main page]

Skip to content

Commit de2f714

Browse files
committed
fix figure.clf and subplot_adjust
1 parent c93957b commit de2f714

File tree

4 files changed

+183
-60
lines changed

4 files changed

+183
-60
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
subplots_adjust has a new ``kwarg``: ``rc_default``
2+
---------------------------------------------------
3+
4+
`.Figure.subplots_adjust` and `.pyplot.subplots_adjust` have a new ``kwarg``:
5+
``rc_default`` that determines the default values for the subplot parameters.
6+
7+
The `.figure.SubplotParams` object has a new get method
8+
:meth:`~.SubplotParams.get_subplot_params`
9+
10+
11+
12+
13+

lib/matplotlib/figure.py

Lines changed: 91 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -173,77 +173,82 @@ def __init__(self, left=None, bottom=None, right=None, top=None,
173173
wspace=None, hspace=None):
174174
"""
175175
All dimensions are fractions of the figure width or height.
176-
Defaults are given by :rc:`figure.subplot.[name]`.
176+
Defaults are given by :rc:`figure.subplot.*`.
177177
178178
Parameters
179179
----------
180-
left : float
180+
left : float, optional
181181
The left side of the subplots of the figure.
182182
183-
right : float
183+
right : float, optional
184184
The right side of the subplots of the figure.
185185
186-
bottom : float
186+
bottom : float, optional
187187
The bottom of the subplots of the figure.
188188
189-
top : float
189+
top : float, optional
190190
The top of the subplots of the figure.
191191
192-
wspace : float
192+
wspace : float, optional
193193
The amount of width reserved for space between subplots,
194194
expressed as a fraction of the average axis width.
195195
196-
hspace : float
196+
hspace : float, optional
197197
The amount of height reserved for space between subplots,
198198
expressed as a fraction of the average axis height.
199199
"""
200200
self.validate = True
201201
self.update(left, bottom, right, top, wspace, hspace)
202202

203+
def __repr__(self):
204+
return ("SubplotParams(left={}, bottom={}, right={}, top={}, "
205+
"wspace={}, hspace={})").format(
206+
self.left, self.bottom, self.right, self.top,
207+
self.wspace, self.hspace)
208+
203209
def update(self, left=None, bottom=None, right=None, top=None,
204-
wspace=None, hspace=None):
205-
"""
206-
Update the dimensions of the passed parameters. *None* means unchanged.
207-
"""
208-
thisleft = getattr(self, 'left', None)
209-
thisright = getattr(self, 'right', None)
210-
thistop = getattr(self, 'top', None)
211-
thisbottom = getattr(self, 'bottom', None)
212-
thiswspace = getattr(self, 'wspace', None)
213-
thishspace = getattr(self, 'hspace', None)
214-
215-
self._update_this('left', left)
216-
self._update_this('right', right)
217-
self._update_this('bottom', bottom)
218-
self._update_this('top', top)
219-
self._update_this('wspace', wspace)
220-
self._update_this('hspace', hspace)
221-
222-
def reset():
223-
self.left = thisleft
224-
self.right = thisright
225-
self.top = thistop
226-
self.bottom = thisbottom
227-
self.wspace = thiswspace
228-
self.hspace = thishspace
210+
wspace=None, hspace=None, rc_default=False):
211+
"""
212+
Update the dimensions of the passed parameters. *None* means
213+
unchanged if the attribute is set and *rc_default* is *False*, and
214+
:rc:`figure.subplot.*` otherwise.
215+
"""
216+
217+
varDict = dict(left=left, bottom=bottom, right=right, top=top,
218+
wspace=wspace, hspace=hspace)
219+
oldVarDict = {key: getattr(self, key, None) for key in varDict.keys()}
229220

221+
self._update(varDict, rc_default)
230222
if self.validate:
231223
if self.left >= self.right:
232-
reset()
224+
self._update(oldVarDict)
233225
raise ValueError('left cannot be >= right')
234226

235227
if self.bottom >= self.top:
236-
reset()
228+
self._update(oldVarDict)
237229
raise ValueError('bottom cannot be >= top')
238230

239-
def _update_this(self, s, val):
240-
if val is None:
241-
val = getattr(self, s, None)
242-
if val is None:
243-
key = 'figure.subplot.' + s
244-
val = rcParams[key]
231+
def _update(self, varDict, rc_default=None):
232+
for att, value in varDict.items():
233+
if value is None:
234+
if not rc_default:
235+
value = getattr(self, att, None)
236+
if value is None:
237+
key = 'figure.subplot.' + att
238+
value = rcParams[key]
245239

246-
setattr(self, s, val)
240+
setattr(self, att, value)
241+
242+
def get_subplot_params(self):
243+
"""
244+
Returns
245+
-------
246+
subplot_params : dictionary
247+
A dictionary with the subplot parameters
248+
"""
249+
subplot_params = self.__dict__.copy()
250+
del subplot_params['validate']
251+
return subplot_params
247252

248253

249254
class Figure(Artist):
@@ -1406,8 +1411,11 @@ def clf(self, keep_observers=False):
14061411
"""
14071412
Clear the figure.
14081413
1409-
Set *keep_observers* to True if, for example,
1410-
a gui widget is tracking the axes in the figure.
1414+
Parameters
1415+
----------
1416+
keep_observers : bool, optional
1417+
Set *keep_observers* to True if, for example,
1418+
a gui widget is tracking the axes in the figure.
14111419
"""
14121420
self.suppressComposite = None
14131421
self.callbacks = cbook.CallbackRegistry()
@@ -1426,6 +1434,7 @@ def clf(self, keep_observers=False):
14261434
self.texts = []
14271435
self.images = []
14281436
self.legends = []
1437+
self.subplotpars.update(rc_default=True)
14291438
if not keep_observers:
14301439
self._axobservers = []
14311440
self._suptitle = None
@@ -1915,13 +1924,48 @@ def colorbar(self, mappable, cax=None, ax=None, use_gridspec=True, **kw):
19151924
return cb
19161925

19171926
def subplots_adjust(self, left=None, bottom=None, right=None, top=None,
1918-
wspace=None, hspace=None):
1927+
wspace=None, hspace=None, rc_default=False):
19191928
"""
1920-
Update the :class:`SubplotParams` with *kwargs* (defaulting to rc when
1921-
*None*) and update the subplot locations.
1929+
Tune the subplots layout by updating the subplots parameters and
1930+
the subplot locations.
1931+
1932+
All dimensions are fractions of the figure width or height.
1933+
1934+
Parameters
1935+
----------
1936+
left : float, optional
1937+
The left side of the subplots of the figure.
1938+
1939+
right : float, optional
1940+
The right side of the subplots of the figure.
1941+
1942+
bottom : float, optional
1943+
The bottom of the subplots of the figure.
19221944
1945+
top : float, optional
1946+
The top of the subplots of the figure.
1947+
1948+
wspace : float, optional
1949+
The amount of width reserved for space between subplots,
1950+
expressed as a fraction of the average axis width.
1951+
1952+
hspace : float, optional
1953+
The amount of height reserved for space between subplots,
1954+
expressed as a fraction of the average axis height.
1955+
1956+
rc_default : bool, optional
1957+
Determine the defaults. *False*, the default, and the values
1958+
are unchanged. *True* and the values are taken from
1959+
:rc:`figure.subplot.*`
1960+
1961+
Notes
1962+
-----
1963+
The subplots parameters are stored in the `~.Figure` attribute
1964+
``subplotpars`` as a `~.SubplotParams` object.
19231965
"""
1924-
self.subplotpars.update(left, bottom, right, top, wspace, hspace)
1966+
1967+
self.subplotpars.update(left, bottom, right, top, wspace,
1968+
hspace, rc_default)
19251969
for ax in self.axes:
19261970
if not isinstance(ax, SubplotBase):
19271971
# Check if sharing a subplots axis

lib/matplotlib/pyplot.py

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,25 +1204,49 @@ def twiny(ax=None):
12041204

12051205

12061206
def subplots_adjust(left=None, bottom=None, right=None, top=None,
1207-
wspace=None, hspace=None):
1207+
wspace=None, hspace=None, rc_default=False):
12081208
"""
1209-
Tune the subplot layout.
1209+
Tune the subplots layout by updating the subplots parameters and
1210+
the subplot locations.
12101211
1211-
The parameter meanings (and suggested defaults) are::
1212+
All dimensions are fractions of the figure width or height.
12121213
1213-
left = 0.125 # the left side of the subplots of the figure
1214-
right = 0.9 # the right side of the subplots of the figure
1215-
bottom = 0.1 # the bottom of the subplots of the figure
1216-
top = 0.9 # the top of the subplots of the figure
1217-
wspace = 0.2 # the amount of width reserved for space between subplots,
1218-
# expressed as a fraction of the average axis width
1219-
hspace = 0.2 # the amount of height reserved for space between subplots,
1220-
# expressed as a fraction of the average axis height
1214+
Parameters
1215+
----------
1216+
left : float, optional
1217+
The left side of the subplots of the figure.
1218+
1219+
right : float, optional
1220+
The right side of the subplots of the figure.
1221+
1222+
bottom : float, optional
1223+
The bottom of the subplots of the figure.
1224+
1225+
top : float, optional
1226+
The top of the subplots of the figure.
1227+
1228+
wspace : float, optional
1229+
The amount of width reserved for space between subplots,
1230+
expressed as a fraction of the average axis width.
12211231
1222-
The actual defaults are controlled by the rc file
1232+
hspace : float, optional
1233+
The amount of height reserved for space between subplots,
1234+
expressed as a fraction of the average axis height.
1235+
1236+
rc_default : bool, optional
1237+
Determine the defaults. *False*, the default, and the values
1238+
are unchanged. *True* and the values are taken from
1239+
:rc:`figure.subplot.*`
1240+
1241+
1242+
Notes
1243+
-----
1244+
The subplots parameters are stored in the `~.Figure` attribute
1245+
``subplotpars`` as a `~.SubplotParams` object.
12231246
"""
1247+
12241248
fig = gcf()
1225-
fig.subplots_adjust(left, bottom, right, top, wspace, hspace)
1249+
fig.subplots_adjust(left, bottom, right, top, wspace, hspace, rc_default)
12261250

12271251

12281252
def subplot_tool(targetfig=None):

lib/matplotlib/tests/test_figure.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,3 +383,45 @@ def test_fspath(fmt, tmpdir):
383383
# All the supported formats include the format name (case-insensitive)
384384
# in the first 100 bytes.
385385
assert fmt.encode("ascii") in file.read(100).lower()
386+
387+
388+
def test_clf_subplotpars():
389+
keys = ('left', 'right', 'bottom', 'top', 'wspace', 'hspace')
390+
rc_params = {key: plt.rcParams['figure.subplot.'+key] for key in keys}
391+
392+
fig = plt.figure(1)
393+
fig.subplots_adjust(left=0.1)
394+
fig.clf()
395+
assert fig.subplotpars.get_subplot_params() == rc_params
396+
397+
398+
def test_suplots_adjust_1():
399+
fig = plt.figure(1)
400+
wspace = 0
401+
fig.subplots_adjust(wspace=wspace)
402+
inDict = dict(left=0.1, right=0.7, bottom=0, top=0.9, hspace=0.05)
403+
fig.subplots_adjust(**inDict)
404+
inDict['wspace'] = wspace
405+
assert fig.subplotpars.get_subplot_params() == inDict
406+
407+
408+
def test_suplots_adjust_2():
409+
fig = plt.figure(1)
410+
fig.subplots_adjust(wspace=0)
411+
inDict = dict(left=0.1, right=0.7, bottom=0, top=0.9, hspace=0.05,
412+
rc_default=True)
413+
fig.subplots_adjust(**inDict)
414+
inDict['wspace'] = plt.rcParams['figure.subplot.wspace']
415+
del inDict['rc_default']
416+
assert fig.subplotpars.get_subplot_params() == inDict
417+
418+
419+
def test_suplots_adjust_plt():
420+
plt.figure(1)
421+
plt.subplots_adjust(wspace=0)
422+
inDict = dict(left=0.1, right=0.7, bottom=0, top=0.9, hspace=0.05,
423+
rc_default=True)
424+
plt.subplots_adjust(**inDict)
425+
inDict['wspace'] = plt.rcParams['figure.subplot.wspace']
426+
del inDict['rc_default']
427+
assert plt.gcf().subplotpars.get_subplot_params() == inDict

0 commit comments

Comments
 (0)
0