Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 541f151

Browse filesBrowse files
committed
fix figure.clf and subplot_adjust
1 parent 9ec4b95 commit 541f151
Copy full SHA for 541f151

File tree

Expand file treeCollapse file tree

4 files changed

+182
-60
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+182
-60
lines changed
+13Lines 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:`.get_subplot_params`
9+
10+
11+
12+
13+

‎lib/matplotlib/figure.py

Copy file name to clipboardExpand all lines: lib/matplotlib/figure.py
+90-47Lines changed: 90 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -173,77 +173,81 @@ 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+
A dictionary with the subplot parameters
247+
"""
248+
subplot_params = self.__dict__.copy()
249+
del subplot_params['validate']
250+
return subplot_params
247251

248252

249253
class Figure(Artist):
@@ -1406,8 +1410,11 @@ def clf(self, keep_observers=False):
14061410
"""
14071411
Clear the figure.
14081412
1409-
Set *keep_observers* to True if, for example,
1410-
a gui widget is tracking the axes in the figure.
1413+
Parameters
1414+
----------
1415+
keep_observers : bool, optional
1416+
Set *keep_observers* to True if, for example,
1417+
a gui widget is tracking the axes in the figure.
14111418
"""
14121419
self.suppressComposite = None
14131420
self.callbacks = cbook.CallbackRegistry()
@@ -1426,6 +1433,7 @@ def clf(self, keep_observers=False):
14261433
self.texts = []
14271434
self.images = []
14281435
self.legends = []
1436+
self.subplotpars.update(rc_default=True)
14291437
if not keep_observers:
14301438
self._axobservers = []
14311439
self._suptitle = None
@@ -1915,13 +1923,48 @@ def colorbar(self, mappable, cax=None, ax=None, use_gridspec=True, **kw):
19151923
return cb
19161924

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

‎lib/matplotlib/pyplot.py

Copy file name to clipboardExpand all lines: lib/matplotlib/pyplot.py
+37-13Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_figure.py
+42Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.