8000 Simplify SubplotParams.update(). · matplotlib/matplotlib@9125cef · GitHub
[go: up one dir, main page]

Skip to content

Commit 9125cef

Browse files
committed
Simplify SubplotParams.update().
by moving initialization, well, to `__init__`. Also improve the class docstring.
1 parent 1020055 commit 9125cef

File tree

1 file changed

+30
-50
lines changed

1 file changed

+30
-50
lines changed

lib/matplotlib/figure.py

Lines changed: 30 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -172,78 +172,58 @@ class SubplotParams:
172172
def __init__(self, left=None, bottom=None, right=None, top=None,
173173
wspace=None, hspace=None):
174174
"""
175-
All dimensions are fractions of the figure width or height.
176175
Defaults are given by :rc:`figure.subplot.[name]`.
177176
178177
Parameters
179178
----------
180179
left : float
181-
The left side of the subplots of the figure.
182-
180+
The position of the left edge of the subplots,
181+
as a fraction of the figure width.
183182
right : float
184-
The right side of the subplots of the figure.
185-
183+
The position of the right edge of the subplots,
184+
as a fraction of the figure width.
186185
bottom : float
187-
The bottom of the subplots of the figure.
188-
186+
The position of the bottom edge of the subplots,
187+
as a fraction of the figure height.
189188
top : float
190-
The top of the subplots of the figure.
191-
189+
The position of the top edge of the subplots,
190+
as a fraction of the figure height.
192191
wspace : float
193-
The amount of width reserved for space between subplots,
194-
expressed as a fraction of the average axis width.
195-
192+
The width of the padding between subplots,
193+
as a fraction of the average axis width.
196194
hspace : float
197-
The amount of height reserved for space between subplots,
198-
expressed as a fraction of the average axis height.
195+
The height of the padding between subplots,
196+
as a fraction of the average axis height.
199197
"""
200198
self.validate = True
199+
for key in ["left", "bottom", "right", "top", "wspace", "hspace"]:
200+
setattr(self, key, rcParams[f"figure.subplot.{key}"])
201201
self.update(left, bottom, right, top, wspace, hspace)
202202

203203
def update(self, left=None, bottom=None, right=None, top=None,
204204
wspace=None, hspace=None):
205205
"""
206206
Update the dimensions of the passed parameters. *None* means unchanged.
207207
"""
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
229-
230208
if self.validate:
231-
if self.left >= self.right:
232-
reset()
209+
if ((left if left is not None else self.left)
210+
>= (right if right is not None else self.right)):
233211
raise ValueError('left cannot be >= right')
234-
235-
if self.bottom >= self.top:
236-
reset()
212+
if ((bottom if bottom is not None else self.bottom)
213+
>= (top if top is not None else self.top)):
237214
raise ValueError('bottom cannot be >= top')
238-
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]
245-
246-
setattr(self, s, val)
215+
if left is not None:
216+
self.left = left
217+
if right is not None:
218+
self.right = right
219+
if bottom is not None:
220+
self.bottom = bottom
221+
if top is not None:
222+
self.top = top
223+
if wspace is not None:
224+
self.wspace = wspace
225+
if hspace is not None:
226+
self.hspace = hspace
247227

248228

249229
class Figure(Artist):

0 commit comments

Comments
 (0)
0