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

Skip to content

Commit 0ff1a40

Browse files
committed
Clarify and check alternative names for axis limits
1 parent f763230 commit 0ff1a40

File tree

3 files changed

+96
-48
lines changed

3 files changed

+96
-48
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Different exception types for undocumented options
2+
--------------------------------------------------
3+
4+
- Passing the undocumented ``xmin`` or ``xmax`` arguments to
5+
:meth:`~matplotlib.axes.Axes.set_xlim` would silently override the ``left``
6+
and ``right`` arguments. :meth:`~matplotlib.axes.Axes.set_ylim` and the
7+
3D equivalents (e.g. :meth:`~mpl_toolkits.axes.Axes3D.set_zlim3d`) had a
8+
corresponding problem.
9+
The ``_min`` and ``_max`` arguments are now deprecated, and a ``TypeError``
10+
will be raised if they would override the earlier limit arguments.

lib/matplotlib/axes/_base.py

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3016,7 +3016,8 @@ def _validate_converted_limits(self, limit, convert):
30163016
raise ValueError("Axis limits cannot be NaN or Inf")
30173017
return converted_limit
30183018

3019-
def set_xlim(self, left=None, right=None, emit=True, auto=False, **kw):
3019+
def set_xlim(self, left=None, right=None, emit=True, auto=False,
3020+
*, xmin=None, xmax=None):
30203021
"""
30213022
Set the data limits for the x-axis
30223023
@@ -3027,6 +3028,9 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False, **kw):
30273028
left : scalar, optional
30283029
The left xlim (default: None, which leaves the left limit
30293030
unchanged).
3031+
The left and right xlims may be passed as the tuple
3032+
(`left`, `right`) as the first positional argument (or as
3033+
the `left` keyword argument).
30303034
30313035
right : scalar, optional
30323036
The right xlim (default: None, which leaves the right limit
@@ -3039,10 +3043,11 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False, **kw):
30393043
Whether to turn on autoscaling of the x-axis. True turns on,
30403044
False turns off (default action), None leaves unchanged.
30413045
3042-
xlimits : tuple, optional
3043-
The left and right xlims may be passed as the tuple
3044-
(`left`, `right`) as the first positional argument (or as
3045-
the `left` keyword argument).
3046+
xmin, xmax : scalar, optional
3047+
These arguments are deprecated and will be removed in a future
3048+
version. They are equivalent to left and right respectively,
3049+
and it is an error to pass both `xmin` and `left` or
3050+
`xmax` and `right`.
30463051
30473052
Returns
30483053
-------
@@ -3073,15 +3078,20 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False, **kw):
30733078
>>> set_xlim(5000, 0)
30743079
30753080
"""
3076-
if 'xmin' in kw:
3077-
left = kw.pop('xmin')
3078-
if 'xmax' in kw:
3079-
right = kw.pop('xmax')
3080-
if kw:
3081-
raise ValueError("unrecognized kwargs: %s" % list(kw))
3082-
30833081
if right is None and iterable(left):
30843082
left, right = left
3083+
if xmin is not None:
3084+
cbook.warn_deprecated('3.0', name='`xmin`',
3085+
alternative='`left`', obj_type='argument')
3086+
if left is not None:
3087+
raise TypeError('Cannot pass both `xmin` and `left`')
3088+
left = xmin
3089+
if xmax is not None:
3090+
cbook.warn_deprecated('3.0', name='`xmax`',
3091+
alternative='`right`', obj_type='argument')
3092+
if right is not None:
3093+
raise TypeError('Cannot pass both `xmax` and `right`')
3094+
right = xmax
30853095

30863096
self._process_unit_info(xdata=(left, right))
30873097
left = self._validate_converted_limits(left, self.convert_xunits)
@@ -3346,7 +3356,8 @@ def get_ylim(self):
33463356
"""
33473357
return tuple(self.viewLim.intervaly)
33483358

3349-
def set_ylim(self, bottom=None, top=None, emit=True, auto=False, **kw):
3359+
def set_ylim(self, bottom=None, top=None, emit=True, auto=False,
3360+
*, ymin=None, ymax=None):
33503361
"""
33513362
Set the data limits for the y-axis
33523363
@@ -3357,6 +3368,9 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False, **kw):
33573368
bottom : scalar, optional
33583369
The bottom ylim (default: None, which leaves the bottom
33593370
limit unchanged).
3371+
The bottom and top ylims may be passed as the tuple
3372+
(`bottom`, `top`) as the first positional argument (or as
3373+
the `bottom` keyword argument).
33603374
33613375
top : scalar, optional
33623376
The top ylim (default: None, which leaves the top limit
@@ -3369,10 +3383,11 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False, **kw):
33693383
Whether to turn on autoscaling of the y-axis. True turns on,
33703384
False turns off (default action), None leaves unchanged.
33713385
3372-
ylimits : tuple, optional
3373-
The bottom and top yxlims may be passed as the tuple
3374-
(`bottom`, `top`) as the first positional argument (or as
3375-
the `bottom` keyword argument).
3386+
ymin, ymax : scalar, optional
3387+
These arguments are deprecated and will be removed in a future
3388+
version. They are equivalent to bottom and top respectively,
3389+
and it is an error to pass both `xmin` and `bottom` or
3390+
`xmax` and `top`.
33763391
33773392
Returns
33783393
-------
@@ -3402,15 +3417,20 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False, **kw):
34023417
34033418
>>> set_ylim(5000, 0)
34043419
"""
3405-
if 'ymin' in kw:
3406-
bottom = kw.pop('ymin')
3407-
if 'ymax' in kw:
3408-
top = kw.pop('ymax')
3409-
if kw:
3410-
raise ValueError("unrecognized kwargs: %s" % list(kw))
3411-
34123420
if top is None and iterable(bottom):
34133421
bottom, top = bottom
3422+
if ymin is not None:
3423+
cbook.warn_deprecated('3.0', name='`ymin`',
3424+
alternative='`bottom`', obj_type='argument')
3425+
if bottom is not None:
3426+
raise TypeError('Cannot pass both `ymin` and `bottom`')
3427+
bottom = ymin
3428+
if ymax is not None:
3429+
cbook.warn_deprecated('3.0', name='`ymax`',
3430+
alternative='`top`', obj_type='argument')
3431+
if top is not None:
3432+
raise TypeError('Cannot pass both `ymax` and `top`')
3433+
top = ymax
34143434

34153435
bottom = self._validate_converted_limits(bottom, self.convert_yunits)
34163436
top = self._validate_converted_limits(top, self.convert_yunits)

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -607,22 +607,28 @@ def _determine_lims(self, xmin=None, xmax=None, *args, **kwargs):
607607
xmax += 0.05
608608
return (xmin, xmax)
609609

610-
def set_xlim3d(self, left=None, right=None, emit=True, auto=False, **kw):
610+
def set_xlim3d(self, left=None, right=None, emit=True, auto=False,
611+
*, xmin=None, xmax=None):
611612
"""
612613
Set 3D x limits.
613614
614615
See :meth:`matplotlib.axes.Axes.set_xlim` for full documentation.
615616
616617
"""
617-
if 'xmin' in kw:
618-
left = kw.pop('xmin')
619-
if 'xmax' in kw:
620-
right = kw.pop('xmax')
621-
if kw:
622-
raise ValueError("unrecognized kwargs: %s" % list(kw))
623-
624618
if right is None and cbook.iterable(left):
625619
left, right = left
620+
if xmin is not None:
621+
cbook.warn_deprecated('3.0', name='`xmin`',
622+
alternative='`left`', obj_type='argument')
623+
if left is not None:
624+
raise TypeError('Cannot pass both `xmin` and `left`')
625+
left = xmin
626+
if xmax is not None:
627+
cbook.warn_deprecated('3.0', name='`xmax`',
628+
alternative='`right`', obj_type='argument')
629+
if right is not None:
630+
raise TypeError('Cannot pass both `xmax` and `right`')
631+
right = xmax
626632

627633
self._process_unit_info(xdata=(left, right))
628634
left = self._validate_converted_limits(left, self.convert_xunits)
@@ -659,22 +665,28 @@ def set_xlim3d(self, left=None, right=None, emit=True, auto=False, **kw):
659665
return left, right
660666
set_xlim = set_xlim3d
661667

662-
def set_ylim3d(self, bottom=None, top=None, emit=True, auto=False, **kw):
668+
def set_ylim3d(self, bottom=None, top=None, emit=True, auto=False,
669+
*, ymin=None, ymax=None):
663670
"""
664671
Set 3D y limits.
665672
666673
See :meth:`matplotlib.axes.Axes.set_ylim` for full documentation.
667674
668675
"""
669-
if 'ymin' in kw:
670-
bottom = kw.pop('ymin')
671-
if 'ymax' in kw:
672-
top = kw.pop('ymax')
673-
if kw:
674-
raise ValueError("unrecognized kwargs: %s" % list(kw))
675-
676676
if top is None and cbook.iterable(bottom):
677677
bottom, top = bottom
678+
if ymin is not None:
679+
cbook.warn_deprecated('3.0', name='`ymin`',
680+
alternative='`bottom`', obj_type='argument')
681+
if bottom is not None:
682+
raise TypeError('Cannot pass both `ymin` and `bottom`')
683+
bottom = ymin
684+
if ymax is not None:
685+
cbook.warn_deprecated('3.0', name='`ymax`',
686+
alternative='`top`', obj_type='argument')
687+
if top is not None:
688+
raise TypeError('Cannot pass both `ymax` and `top`')
689+
top = ymax
678690

679691
self._process_unit_info(ydata=(bottom, top))
680692
bottom = self._validate_converted_limits(bottom, self.convert_yunits)
@@ -711,22 +723,28 @@ def set_ylim3d(self, bottom=None, top=None, emit=True, auto=False, **kw):
711723
return bottom, top
712724
set_ylim = set_ylim3d
713725

714-
def set_zlim3d(self, bottom=None, top=None, emit=True, auto=False, **kw):
726+
def set_zlim3d(self, bottom=None, top=None, emit=True, auto=False,
727+
*, zmin=None, zmax=None):
715728
"""
716729
Set 3D z limits.
717730
718731
See :meth:`matplotlib.axes.Axes.set_ylim` for full documentation
719732
720733
"""
721-
if 'zmin' in kw:
722-
bottom = kw.pop('zmin')
723-
if 'zmax' in kw:
724-
top = kw.pop('zmax')
725-
if kw:
726-
raise ValueError("unrecognized kwargs: %s" % list(kw))
727-
728734
if top is None and cbook.iterable(bottom):
729735
bottom, top = bottom
736+
if zmin is not None:
737+
cbook.warn_deprecated('3.0', name='`zmin`',
738+
alternative='`bottom`', obj_type='argument')
739+
if bottom is not None:
740+
raise TypeError('Cannot pass both `zmin` and `bottom`')
741+
bottom = zmin
742+
if zmax is not None:
743+
cbook.warn_deprecated('3.0', name='`zmax`',
744+
alternative='`top`', obj_type='argument')
745+
if top is not None:
746+
raise TypeError('Cannot pass both `zmax` and `top`')
747+
top = zmax
730748

731749
self._process_unit_info(zdata=(bottom, top))
732750
bottom = self._validate_converted_limits(bottom, self.convert_zunits)

0 commit comments

Comments
 (0)