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 1afff75

Browse filesBrowse files
committed
fix figure.clf and subplot_adjust
1 parent 0984694 commit 1afff75
Copy full SHA for 1afff75

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
@@ -172,77 +172,81 @@ def __init__(self, left=None, bottom=None, right=None, top=None,
172172
wspace=None, hspace=None):
173173
"""
174174
All dimensions are fractions of the figure width or height.
175-
Defaults are given by :rc:`figure.subplot.[name]`.
175+
Defaults are given by :rc:`figure.subplot.*`.
176176
177177
Parameters
178178
----------
179-
left : float
179+
left : float, optional
180180
The left side of the subplots of the figure.
181181
182-
right : float
182+
right : float, optional
183183
The right side of the subplots of the figure.
184184
185-
bottom : float
185+
bottom : float, optional
186186
The bottom of the subplots of the figure.
187187
188-
top : float
188+
top : float, optional
189189
The top of the subplots of the figure.
190190
191-
wspace : float
191+
wspace : float, optional
192192
The amount of width reserved for space between subplots,
193193
expressed as a fraction of the average axis width.
194194
195-
hspace : float
195+
hspace : float, optional
196196
The amount of height reserved for space between subplots,
197197
expressed as a fraction of the average axis height.
198198
"""
199199
self.validate = True
200200
self.update(left, bottom, right, top, wspace, hspace)
201201

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

220+
self._update(varDict, rc_default)
229221
if self.validate:
230222
if self.left >= self.right:
231-
reset()
223+
self._update(oldVarDict)
232224
raise ValueError('left cannot be >= right')
233225

234226
if self.bottom >= self.top:
235-
reset()
227+
self._update(oldVarDict)
236228
raise ValueError('bottom cannot be >= top')
237229

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

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

247251

248252
class Figure(Artist):
@@ -1396,8 +1400,11 @@ def clf(self, keep_observers=False):
13961400
"""
13971401
Clear the figure.
13981402
1399-
Set *keep_observers* to True if, for example,
1400-
a gui widget is tracking the axes in the figure.
1403+
Parameters
1404+
----------
1405+
keep_observers : bool, optional
1406+
Set *keep_observers* to True if, for example,
1407+
a gui widget is tracking the axes in the figure.
14011408
"""
14021409
self.suppressComposite = None
14031410
self.callbacks = cbook.CallbackRegistry()
@@ -1416,6 +1423,7 @@ def clf(self, keep_observers=False):
14161423
self.texts = []
14171424
self.images = []
14181425
self.legends = []
1426+
self.subplotpars.update(rc_default=True)
14191427
if not keep_observers:
14201428
self._axobservers = []
14211429
self._suptitle = None
@@ -1904,13 +1912,48 @@ def colorbar(self, mappable, cax=None, ax=None, use_gridspec=True, **kw):
19041912
return cb
19051913

19061914
def subplots_adjust(self, left=None, bottom=None, right=None, top=None,
1907-
wspace=None, hspace=None):
1915+
wspace=None, hspace=None, rc_default=False):
19081916
"""
1909-
Update the :class:`SubplotParams` with *kwargs* (defaulting to rc when
1910-
*None*) and update the subplot locations.
1917+
Tune the subplots layout by updating the subplots parameters and
1918+
the subplot locations.
1919+
1920+
All dimensions are fractions of the figure width or height.
1921+
1922+
Parameters
1923+
----------
1924+
left : float, optional
1925+
The left side of the subplots of the figure.
1926+
1927+
right : float, optional
1928+
The right side of the subplots of the figure.
1929+
1930+
bottom : float, optional
1931+
The bottom of the subplots of the figure.
19111932
1933+
top : float, optional
1934+
The top of the subplots of the figure.
1935+
1936+
wspace : float, optional
1937+
The amount of width reserved for space between subplots,
1938+
expressed as a fraction of the average axis width.
1939+
1940+
hspace : float, optional
1941+
The amount of height reserved for space between subplots,
1942+
expressed as a fraction of the average axis height.
1943+
1944+
rc_default : bool, optional
1945+
Determine the defaults. *False*, the default, and the values
1946+
are unchanged. *True* and the values are taken from
1947+
:rc:`figure.subplot.*`
1948+
1949+
Notes
1950+
-----
1951+
The subplots parameters are stored in the `~.Figure` attribute
1952+
``subplotpars`` as a `~.SubplotParams` object.
19121953
"""
1913-
self.subplotpars.update(left, bottom, right, top, wspace, hspace)
1954+
1955+
self.subplotpars.update(left, bottom, right, top, wspace,
1956+
hspace, rc_default)
19141957
for ax in self.axes:
19151958
if not isinstance(ax, SubplotBase):
19161959
# 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
@@ -1202,25 +1202,49 @@ def twiny(ax=None):
12021202

12031203

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

12251249

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