Skip to content

Commit 1627f2a

Browse files
committed
Clarify and check alternative names for axis limits
1 parent 6ad6955 commit 1627f2a

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 yxlims 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
@@ -610,22 +610,28 @@ def _determine_lims(self, xmin=None, xmax=None, *args, **kwargs):
610610
xmax += 0.05
611611
return (xmin, xmax)
612612

613-
def set_xlim3d(self, left=None, right=None, emit=True, auto=False, **kw):
613+
def set_xlim3d(self, left=None, right=None, emit=True, auto=False,
614+
*, xmin=None, xmax=None):
614615
"""
615616
Set 3D x limits.
616617
617618
See :meth:`matplotlib.axes.Axes.set_xlim` for full documentation.
618619
619620
"""
620-
if 'xmin' in kw:
621-
left = kw.pop('xmin')
622-
if 'xmax' in kw:
623-
right = kw.pop('xmax')
624-
if kw:
625-
raise ValueError("unrecognized kwargs: %s" % list(kw))
626-
627621
if right is None and cbook.iterable(left):
628622
left, right = left
623+
if xmin is not None:
624+
cbook.warn_deprecated('3.0', name='`xmin`',
625+
alternative='`left`', obj_type='argument')
626+
if left is not None:
627+
raise TypeError('Cannot pass both `xmin` and `left`')
628+
left = xmin
629+
if xmax is not None:
630+
cbook.warn_deprecated('3.0', name='`xmax`',
631+
alternative='`right`', obj_type='argument')
632+
if right is not None:
633+
raise TypeError('Cannot pass both `xmax` and `right`')
634+
right = xmax
629635

630636
self._process_unit_info(xdata=(left, right))
631637
left = self._validate_converted_limits(left, self.convert_xunits)
@@ -662,22 +668,28 @@ def set_xlim3d(self, left=None, right=None, emit=True, auto=False, **kw):
662668
return left, right
663669
set_xlim = set_xlim3d
664670

665-
def set_ylim3d(self, bottom=None, top=None, emit=True, auto=False, **kw):
671+
def set_ylim3d(self, bottom=None, top=None, emit=True, auto=False,
672+
*, ymin=None, ymax=None):
666673
"""
667674
Set 3D y limits.
668675
669676
See :meth:`matplotlib.axes.Axes.set_ylim` for full documentation.
670677
671678
"""
672-
if 'ymin' in kw:
673-
bottom = kw.pop('ymin')
674-
if 'ymax' in kw:
675-
top = kw.pop('ymax')
676-
if kw:
677-
raise ValueError("unrecognized kwargs: %s" % list(kw))
678-
679679
if top is None and cbook.iterable(bottom):
680680
bottom, top = bottom
681+
if ymin is not None:
682+
cbook.warn_deprecated('3.0', name='`ymin`',
683+
alternative='`bottom`', obj_type='argument')
684+
if bottom is not None:
685+
raise TypeError('Cannot pass both `ymin` and `bottom`')
686+
bottom = ymin
687+
if ymax is not None:
688+
cbook.warn_deprecated('3.0', name='`ymax`',
689+
alternative='`top`', obj_type='argument')
690+
if top is not None:
691+
raise TypeError('Cannot pass both `ymax` and `top`')
692+
top = ymax
681693

682694
self._process_unit_info(ydata=(bottom, top))
683695
bottom = self._validate_converted_limits(bottom, self.convert_yunits)
@@ -714,22 +726,28 @@ def set_ylim3d(self, bottom=None, top=None, emit=True, auto=False, **kw):
714726
return bottom, top
715727
set_ylim = set_ylim3d
716728

717-
def set_zlim3d(self, bottom=None, top=None, emit=True, auto=False, **kw):
729+
def set_zlim3d(self, bottom=None, top=None, emit=True, auto=False,
730+
*, zmin=None, zmax=None):
718731
"""
719732
Set 3D z limits.
720733
721734
See :meth:`matplotlib.axes.Axes.set_ylim` for full documentation
722735
723736
"""
724-
if 'zmin' in kw:
725-
bottom = kw.pop('zmin')
726-
if 'zmax' in kw:
727-
top = kw.pop('zmax')
728-
if kw:
729-
raise ValueError("unrecognized kwargs: %s" % list(kw))
730-
731737
if top is None and cbook.iterable(bottom):
732738
bottom, top = bottom
739+
if zmin is not None:
740+
cbook.warn_deprecated('3.0', name='`zmin`',
741+
alternative='`bottom`', obj_type='argument')
742+
if bottom is not None:
743+
raise TypeError('Cannot pass both `zmin` and `bottom`')
744+
bottom = zmin
745+
if zmax is not None:
746+
cbook.warn_deprecated('3.0', name='`zmax`',
747+
alternative='`top`', obj_type='argument')
748+
if top is not None:
749+
raise TypeError('Cannot pass both `zmax` and `top`')
750+
top = zmax
733751

734752
self._process_unit_info(zdata=(bottom, top))
735753
bottom = self._validate_converted_limits(bottom, self.convert_zunits)

0 commit comments

Comments
 (0)