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

Skip to content

Commit 4a12a60

Browse files
authored
Merge pull request #11137 from Zac-HD/issue-9912-cleanup
Convert **kwargs to named arguments for a clearer API
2 parents 71b181f + c408392 commit 4a12a60

File tree

5 files changed

+124
-92
lines changed

5 files changed

+124
-92
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Different exception types for undocumented options
2+
--------------------------------------------------
3+
4+
- Passing ``style='comma'`` to :meth:`~matplotlib.axes.Axes.ticklabel_format`
5+
was never supported. It now raises ``ValueError`` like all other
6+
unsupported styles, rather than ``NotImplementedError``.
7+
8+
- Passing the undocumented ``xmin`` or ``xmax`` arguments to
9+
:meth:`~matplotlib.axes.Axes.set_xlim` would silently override the ``left``
10+
and ``right`` arguments. :meth:`~matplotlib.axes.Axes.set_ylim` and the
11+
3D equivalents (e.g. :meth:`~mpl_toolkits.axes.Axes3D.set_zlim3d`) had a
12+
corresponding problem.
13+
The ``_min`` and ``_max`` arguments are now deprecated, and a ``TypeError``
14+
will be raised if they would override the earlier limit arguments.

lib/matplotlib/axes/_base.py

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2756,8 +2756,6 @@ def ticklabel_format(self, *, axis='both', style='', scilimits=None,
27562756
sb = True
27572757
elif style == 'plain':
27582758
sb = False
2759-
elif style == 'comma':
2760-
raise NotImplementedError("comma style remains to be added")
27612759
elif style == '':
27622760
sb = None
27632761
else:
@@ -3028,7 +3026,8 @@ def _validate_converted_limits(self, limit, convert):
30283026
raise ValueError("Axis limits cannot be NaN or Inf")
30293027
return converted_limit
30303028

3031-
def set_xlim(self, left=None, right=None, emit=True, auto=False, **kw):
3029+
def set_xlim(self, left=None, right=None, emit=True, auto=False,
3030+
*, xmin=None, xmax=None):
30323031
"""
30333032
Set the data limits for the x-axis
30343033
@@ -3039,6 +3038,9 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False, **kw):
30393038
left : scalar, optional
30403039
The left xlim (default: None, which leaves the left limit
30413040
unchanged).
3041+
The left and right xlims may be passed as the tuple
3042+
(`left`, `right`) as the first positional argument (or as
3043+
the `left` keyword argument).
30423044
30433045
right : scalar, optional
30443046
The right xlim (default: None, which leaves the right limit
@@ -3051,10 +3053,11 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False, **kw):
30513053
Whether to turn on autoscaling of the x-axis. True turns on,
30523054
False turns off (default action), None leaves unchanged.
30533055
3054-
xlimits : tuple, optional
3055-
The left and right xlims may be passed as the tuple
3056-
(`left`, `right`) as the first positional argument (or as
3057-
the `left` keyword argument).
3056+
xmin, xmax : scalar, optional
3057+
These arguments are deprecated and will be removed in a future
3058+
version. They are equivalent to left and right respectively,
3059+
and it is an error to pass both `xmin` and `left` or
3060+
`xmax` and `right`.
30583061
30593062
Returns
30603063
-------
@@ -3085,15 +3088,20 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False, **kw):
30853088
>>> set_xlim(5000, 0)
30863089
30873090
"""
3088-
if 'xmin' in kw:
3089-
left = kw.pop('xmin')
3090-
if 'xmax' in kw:
3091-
right = kw.pop('xmax')
3092-
if kw:
3093-
raise ValueError("unrecognized kwargs: %s" % list(kw))
3094-
30953091
if right is None and iterable(left):
30963092
left, right = left
3093+
if xmin is not None:
3094+
cbook.warn_deprecated('3.0', name='`xmin`',
3095+
alternative='`left`', obj_type='argument')
3096+
if left is not None:
3097+
raise TypeError('Cannot pass both `xmin` and `left`')
3098+
left = xmin
3099+
if xmax is not None:
3100+
cbook.warn_deprecated('3.0', name='`xmax`',
3101+
alternative='`right`', obj_type='argument')
3102+
if right is not None:
3103+
raise TypeError('Cannot pass both `xmax` and `right`')
3104+
right = xmax
30973105

30983106
self._process_unit_info(xdata=(left, right))
30993107
left = self._validate_converted_limits(left, self.convert_xunits)
@@ -3358,7 +3366,8 @@ def get_ylim(self):
33583366
"""
33593367
return tuple(self.viewLim.intervaly)
33603368

3361-
def set_ylim(self, bottom=None, top=None, emit=True, auto=False, **kw):
3369+
def set_ylim(self, bottom=None, top=None, emit=True, auto=False,
3370+
*, ymin=None, ymax=None):
33623371
"""
33633372
Set the data limits for the y-axis
33643373
@@ -3369,6 +3378,9 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False, **kw):
33693378
bottom : scalar, optional
33703379
The bottom ylim (default: None, which leaves the bottom
33713380
limit unchanged).
3381+
The bottom and top ylims may be passed as the tuple
3382+
(`bottom`, `top`) as the first positional argument (or as
3383+
the `bottom` keyword argument).
33723384
33733385
top : scalar, optional
33743386
The top ylim (default: None, which leaves the top limit
@@ -3381,10 +3393,11 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False, **kw):
33813393
Whether to turn on autoscaling of the y-axis. True turns on,
33823394
False turns off (default action), None leaves unchanged.
33833395
3384-
ylimits : tuple, optional
3385-
The bottom and top yxlims may be passed as the tuple
3386-
(`bottom`, `top`) as the first positional argument (or as
3387-
the `bottom` keyword argument).
3396+
ymin, ymax : scalar, optional
3397+
These arguments are deprecated and will be removed in a future
3398+
version. They are equivalent to bottom and top respectively,
3399+
and it is an error to pass both `xmin` and `bottom` or
3400+
`xmax` and `top`.
33883401
33893402
Returns
33903403
-------
@@ -3414,15 +3427,20 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False, **kw):
34143427
34153428
>>> set_ylim(5000, 0)
34163429
"""
3417-
if 'ymin' in kw:
3418-
bottom = kw.pop('ymin')
3419-
if 'ymax' in kw:
3420-
top = kw.pop('ymax')
3421-
if kw:
3422-
raise ValueError("unrecognized kwargs: %s" % list(kw))
3423-
34243430
if top is None and iterable(bottom):
34253431
bottom, top = bottom
3432+
if ymin is not None:
3433+
cbook.warn_deprecated('3.0', name='`ymin`',
3434+
alternative='`bottom`', obj_type='argument')
3435+
if bottom is not None:
3436+
raise TypeError('Cannot pass both `ymin` and `bottom`')
3437+
bottom = ymin
3438+
if ymax is not None:
3439+
cbook.warn_deprecated('3.0', name='`ymax`',
3440+
alternative='`top`', obj_type='argument')
3441+
if top is not None:
3442+
raise TypeError('Cannot pass both `ymax` and `top`')
3443+
top = ymax
34263444

34273445
bottom = self._validate_converted_limits(bottom, self.convert_yunits)
34283446
top = self._validate_converted_limits(top, self.convert_yunits)

lib/matplotlib/axis.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -334,12 +334,10 @@ def get_view_interval(self):
334334
raise NotImplementedError('Derived must override')
335335

336336
def _apply_params(self, **kw):
337-
switchkw = ['gridOn', 'tick1On', 'tick2On', 'label1On', 'label2On']
338-
switches = [k for k in kw if k in switchkw]
339-
for k in switches:
340-
setattr(self, k, kw.pop(k))
341-
newmarker = [k for k in kw if k in ['size', 'width', 'pad', 'tickdir']]
342-
if newmarker:
337+
for name in ['gridOn', 'tick1On', 'tick2On', 'label1On', 'label2On']:
338+
if name in kw:
339+
setattr(self, name, kw.pop(name))
340+
if any(k in kw for k in ['size', 'width', 'pad', 'tickdir']):
343341
self._size = kw.pop('size', self._size)
344342
# Width could be handled outside this block, but it is
345343
# convenient to leave it here.

lib/matplotlib/pyplot.py

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,19 +1365,19 @@ def ylim(*args, **kwargs):
13651365
return ret
13661366

13671367

1368-
def xticks(*args, **kwargs):
1368+
def xticks(ticks=None, labels=None, **kwargs):
13691369
"""
13701370
Get or set the current tick locations and labels of the x-axis.
13711371
13721372
Call signatures::
13731373
13741374
locs, labels = xticks() # Get locations and labels
13751375
1376-
xticks(locs, [labels], **kwargs) # Set locations and labels
1376+
xticks(ticks, [labels], **kwargs) # Set locations and labels
13771377
13781378
Parameters
13791379
----------
1380-
locs : array_like
1380+
ticks : array_like
13811381
A list of positions at which ticks should be placed. You can pass an
13821382
empty list to disable xticks.
13831383
@@ -1427,36 +1427,34 @@ def xticks(*args, **kwargs):
14271427
"""
14281428
ax = gca()
14291429

1430-
if len(args) == 0:
1430+
if ticks is None and labels is None:
14311431
locs = ax.get_xticks()
14321432
labels = ax.get_xticklabels()
1433-
elif len(args) == 1:
1434-
locs = ax.set_xticks(args[0])
1433+
elif labels is None:
1434+
locs = ax.set_xticks(ticks)
14351435
labels = ax.get_xticklabels()
1436-
elif len(args) == 2:
1437-
locs = ax.set_xticks(args[0])
1438-
labels = ax.set_xticklabels(args[1], **kwargs)
14391436
else:
1440-
raise TypeError('Illegal number of arguments to xticks')
1437+
locs = ax.set_xticks(ticks)
1438+
labels = ax.set_xticklabels(labels, **kwargs)
14411439
for l in labels:
14421440
l.update(kwargs)
14431441

14441442
return locs, silent_list('Text xticklabel', labels)
14451443

14461444

1447-
def yticks(*args, **kwargs):
1445+
def yticks(ticks=None, labels=None, **kwargs):
14481446
"""
14491447
Get or set the current tick locations and labels of the y-axis.
14501448
14511449
Call signatures::
14521450
14531451
locs, labels = yticks() # Get locations and labels
14541452
1455-
yticks(locs, [labels], **kwargs) # Set locations and labels
1453+
yticks(ticks, [labels], **kwargs) # Set locations and labels
14561454
14571455
Parameters
14581456
----------
1459-
locs : array_like
1457+
ticks : array_like
14601458
A list of positions at which ticks should be placed. You can pass an
14611459
empty list to disable yticks.
14621460
@@ -1506,17 +1504,15 @@ def yticks(*args, **kwargs):
15061504
"""
15071505
ax = gca()
15081506

1509-
if len(args) == 0:
1507+
if ticks is None and labels is None:
15101508
locs = ax.get_yticks()
15111509
labels = ax.get_yticklabels()
1512-
elif len(args) == 1:
1513-
locs = ax.set_yticks(args[0])
1510+
elif labels is None:
1511+
locs = ax.set_yticks(ticks)
15141512
labels = ax.get_yticklabels()
1515-
elif len(args) == 2:
1516-
locs = ax.set_yticks(args[0])
1517-
labels = ax.set_yticklabels(args[1], **kwargs)
15181513
else:
1519-
raise TypeError('Illegal number of arguments to yticks')
1514+
locs = ax.set_yticks(ticks)
1515+
labels = ax.set_yticklabels(labels, **kwargs)
15201516
for l in labels:
15211517
l.update(kwargs)
15221518

0 commit comments

Comments
 (0)