Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit da5f85b

Browse files
committed
Explicit args and refactor Axes.margins
1 parent 213f399 commit da5f85b

File tree

3 files changed

+55
-52
lines changed

3 files changed

+55
-52
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Explicit arguments instead of \*args, \*\*kwargs
2+
------------------------------------------------
3+
4+
:PEP:`3102` describes keyword-only arguments, which allow Matplotlib
5+
to provide explicit call signatures - where we previously used
6+
``*args, **kwargs`` and ``kwargs.pop``, we can now expose named
7+
arguments. In some places, unknown kwargs were previously ignored but
8+
now raise ``TypeError`` because ``**kwargs`` has been removed.
9+
10+
- :meth:`matplotlib.axes.Axes.margins` and
11+
:meth:`mpl_toolkits.mplot3d.Axes3D.margins` no longer accept
12+
arbitrary keywords. ``x``, ``y``, and (for Axes3D) ``z`` may
13+
be passed as either keyword or positional arguments with the
14+
same behaviour as before.

lib/matplotlib/axes/_base.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2229,7 +2229,7 @@ def set_ymargin(self, m):
22292229
self._ymargin = m
22302230
self.stale = True
22312231

2232-
def margins(self, *args, **kw):
2232+
def margins(self, x_or_both=None, y=None, *, x=None, tight=True):
22332233
"""
22342234
Set or retrieve autoscaling margins.
22352235
@@ -2267,27 +2267,26 @@ def margins(self, *args, **kw):
22672267
it is used in autoscaling.
22682268
22692269
"""
2270-
if not args and not kw:
2270+
if x_or_both is not None:
2271+
if x is not None:
2272+
warnings.warn('x_or_both=%r overrides x=%r' % (x_or_both, x))
2273+
x = x_or_both
2274+
if x_or_both is not None and y is None:
2275+
y = x_or_both
2276+
2277+
if x is None and y is None:
2278+
if tight is not True:
2279+
warnings.warn('ignoring tight=%r in get mode' % (tight,))
22712280
return self._xmargin, self._ymargin
22722281

2273-
tight = kw.pop('tight', True)
2274-
mx = kw.pop('x', None)
2275-
my = kw.pop('y', None)
2276-
if len(args) == 1:
2277-
mx = my = args[0]
2278-
elif len(args) == 2:
2279-
mx, my = args
2280-
elif len(args) > 2:
2281-
raise ValueError("more than two arguments were supplied")
2282-
if mx is not None:
2283-
self.set_xmargin(mx)
2284-
if my is not None:
2285-
self.set_ymargin(my)
2286-
2287-
scalex = (mx is not None)
2288-
scaley = (my is not None)
2282+
if x is not None:
2283+
self.set_xmargin(x)
2284+
if y is not None:
2285+
self.set_ymargin(y)
22892286

2290-
self.autoscale_view(tight=tight, scalex=scalex, scaley=scaley)
2287+
self.autoscale_view(
2288+
tight=tight, scalex=(x is not None), scaley=(y is not None)
2289+
)
22912290

22922291
def set_rasterization_zorder(self, z):
22932292
"""

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ def set_zmargin(self, m):
384384
self._zmargin = m
385385
self.stale = True
386386

387-
def margins(self, *args, **kw):
387+
def margins(self, x_or_both=None, y=None, z=None, *, x=None, tight=True):
388388
"""
389389
Convenience method to set or retrieve autoscaling margins.
390390
@@ -420,41 +420,31 @@ def margins(self, *args, **kw):
420420
.. versionadded :: 1.1.0
421421
This function was added, but not tested. Please report any bugs.
422422
"""
423-
if not args and not kw:
423+
if x_or_both is not None:
424+
if x is not None:
425+
warnings.warn('x_or_both=%r overrides x=%r' % (x_or_both, x))
426+
x = x_or_both
427+
if y is None:
428+
y = x_or_both
429+
if z is None:
430+
z = x_or_both
431+
432+
if x is None and y is None and z is None:
433+
if tight is not True:
434+
warnings.warn('ignoring tight=%r in get mode' % (tight,))
424435
return self._xmargin, self._ymargin, self._zmargin
425436

426-
tight = kw.pop('tight', True)
427-
mx = kw.pop('x', None)
428-
my = kw.pop('y', None)
429-
mz = kw.pop('z', None)
430-
if not args:
431-
pass
432-
elif len(args) == 1:
433-
mx = my = mz = args[0]
434-
elif len(args) == 2:
435-
warnings.warn(
436-
"Passing exactly two positional arguments to Axes3D.margins "
437-
"is deprecated. If needed, pass them as keyword arguments "
438-
"instead", cbook.mplDeprecation)
439-
mx, my = args
440-
elif len(args) == 3:
441-
mx, my, mz = args
442-
else:
443-
raise ValueError(
444-
"Axes3D.margins takes at most three positional arguments")
445-
if mx is not None:
446-
self.set_xmargin(mx)
447-
if my is not None:
448-
self.set_ymargin(my)
449-
if mz is not None:
450-
self.set_zmargin(mz)
451-
452-
scalex = mx is not None
453-
scaley = my is not None
454-
scalez = mz is not None
437+
if x is not None:
438+
self.set_xmargin(x)
439+
if y is not None:
440+
self.set_ymargin(y)
441+
if z is not None:
442+
self.set_zmargin(z)
455443

456-
self.autoscale_view(tight=tight, scalex=scalex, scaley=scaley,
457-
scalez=scalez)
444+
self.autoscale_view(
445+
tight=tight, scalex=(x is not None), scaley=(y is not None),
446+
scalez=(z is not None)
447+
)
458448

459449
def autoscale(self, enable=True, axis='both', tight=None):
460450
"""

0 commit comments

Comments
 (0)