Merge pull request #11146 from Zac-HD/refactor-margins · tropfcode/matplotlib@bc2ea01 · GitHub
Skip to content

Commit bc2ea01

Browse files
authored
Merge pull request matplotlib#11146 from Zac-HD/refactor-margins
Explicit args and refactor Axes.margins
2 parents 4536165 + 17e60a4 commit bc2ea01

3 files changed

Lines changed: 69 additions & 56 deletions

File tree

Lines changed: 13 additions & 0 deletions

lib/matplotlib/axes/_base.py

Lines changed: 25 additions & 21 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, *margins, x=None, y=None, tight=True):
22332233
"""
22342234
Set or retrieve autoscaling margins.
22352235
@@ -2250,10 +2250,11 @@ def margins(self, *args, **kw):
22502250
margins(..., tight=False)
22512251
22522252
All three forms above set the xmargin and ymargin parameters.
2253-
All keyword parameters are optional. A single argument
2253+
All keyword parameters are optional. A single positional argument
22542254
specifies both xmargin and ymargin. The padding added to the end of
22552255
each interval is *margin* times the data interval. The *margin* must
2256-
be a float in the range [0, 1].
2256+
be a float in the range [0, 1]. Passing both positional and keyword
2257+
arguments for xmargin and/or ymargin is invalid.
22572258
22582259
The *tight* parameter is passed to :meth:`autoscale_view`
22592260
, which is executed after a margin is changed; the default here is
@@ -2267,27 +2268,30 @@ def margins(self, *args, **kw):
22672268
it is used in autoscaling.
22682269
22692270
"""
2270-
if not args and not kw:
2271+
if margins and x is not None and y is not None:
2272+
raise TypeError('Cannot pass both positional and keyword '
2273+
'arguments for x and/or y.')
2274+
elif len(margins) == 1:
2275+
x = y = margins[0]
2276+
elif len(margins) == 2:
2277+
x, y = margins
2278+
elif margins:
2279+
raise TypeError('Must pass a single positional argument for all '
2280+
'margins, or one for each margin (x, y).')
2281+
2282+
if x is None and y is None:
2283+
if tight is not True:
2284+
warnings.warn('ignoring tight=%r in get mode' % (tight,))
22712285
return self._xmargin, self._ymargin
22722286

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)
2287+
if x is not None:
2288+
self.set_xmargin(x)
2289+
if y is not None:
2290+
self.set_ymargin(y)
22892291

2290-
self.autoscale_view(tight=tight, scalex=scalex, scaley=scaley)
2292+
self.autoscale_view(
2293+
tight=tight, scalex=(x is not None), scaley=(y is not None)
2294+
)
22912295

22922296
def set_rasterization_zorder(self, z):
22932297
"""

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 31 additions & 35 deletions

0 commit comments

Comments
 (0)