8000 API: make y/rlims out of order raise a Value Error · matplotlib/matplotlib@24ca248 · GitHub
[go: up one dir, main page]

Skip to content

Commit 24ca248

Browse files
committed
API: make y/rlims out of order raise a Value Error
DOC: cleanup dosctrings TST: added test for ValueError
1 parent 0f799b6 commit 24ca248

File tree

3 files changed

+103
-18
lines changed

3 files changed

+103
-18
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3083,8 +3083,8 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False,
30833083
The left xlim (default: None, which leaves the left limit
30843084
unchanged).
30853085
The left and right xlims may be passed as the tuple
3086-
(`left`, `right`) as the first positional argument (or as
3087-
the `left` keyword argument).
3086+
(*left*, *right*) as the first positional argument (or as
3087+
the *left* keyword argument).
30883088
30893089
right : scalar, optional
30903090
The right xlim (default: None, which leaves the right limit
@@ -3100,17 +3100,17 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False,
31003100
xmin, xmax : scalar, optional
31013101
These arguments are deprecated and will be removed in a future
31023102
version. They are equivalent to left and right respectively,
3103-
and it is an error to pass both `xmin` and `left` or
3104-
`xmax` and `right`.
3103+
and it is an error to pass both *xmin* and *left* or
3104+
*xmax* and *right*.
31053105
31063106
Returns
31073107
-------
31083108
xlimits : tuple
3109-
Returns the new x-axis limits as (`left`, `right`).
3109+
Returns the new x-axis limits as (*left*, *right*).
31103110
31113111
Notes
31123112
-----
3113-
The `left` value may be greater than the `right` value, in which
3113+
The *left* value may be greater than the *right* value, in which
31143114
case the x-axis values will decrease from left to right.
31153115
31163116
Examples
@@ -3124,7 +3124,7 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False,
31243124
>>> set_xlim(right=right_lim)
31253125
31263126
Limits may be passed in reverse order to flip the direction of
3127-
the x-axis. For example, suppose `x` represents the number of
3127+
the x-axis. For example, suppose *x* represents the number of
31283128
years before present. The x-axis limits might be set like the
31293129
following so 5000 years ago is on the left of the plot and the
31303130
present is on the right.
@@ -3417,15 +3417,15 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False,
34173417
The bottom ylim (default: None, which leaves the bottom
34183418
limit unchanged).
34193419
The bottom and top ylims may be passed as the tuple
3420-
(`bottom`, `top`) as the first positional argument (or as
3421-
the `bottom` keyword argument).
3420+
(*bottom*, *top*) as the first positional argument (or as
3421+
the *bottom* keyword argument).
34223422
34233423
top : scalar, optional
34243424
The top ylim (default: None, which leaves the top limit
34253425
unchanged).
34263426
34273427
emit : bool, optional
3428-
Whether to notify observers of limit change (default: True).
3428+
Whether to notify observers of limit change (default: ``True``).
34293429
34303430
auto : bool or None, optional
34313431
Whether to turn on autoscaling of the y-axis. True turns on,
@@ -3434,17 +3434,17 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False,
34343434
ymin, ymax : scalar, optional
34353435
These arguments are deprecated and will be removed in a future
34363436
version. They are equivalent to bottom and top respectively,
3437-
and it is an error to pass both `xmin` and `bottom` or
3438-
`xmax` and `top`.
3437+
and it is an error to pass both *ymin* and *bottom* or
3438+
*ymax* and *top*.
34393439
34403440
Returns
34413441
-------
34423442
ylimits : tuple
3443-
Returns the new y-axis limits as (`bottom`, `top`).
3443+
Returns the new y-axis limits as (*bottom*, *top*).
34443444
34453445
Notes
34463446
-----
3447-
The `bottom` value may be greater than the `top` value, in which
3447+
The *bottom* value may be greater than the *top* value, in which
34483448
case the y-axis values will decrease from bottom to top.
34493449
34503450
Examples

lib/matplotlib/projections/polar.py

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import types
33

44
import numpy as np
5+
import warnings
56

67
from matplotlib.axes import Axes
78
import matplotlib.axis as maxis
@@ -1182,12 +1183,87 @@ def set_rorigin(self, rorigin):
11821183
def get_rorigin(self):
11831184
return self._originViewLim.y0
11841185

1185-
def set_rlim(self, *args, **kwargs):
1186+
def set_rlim(self, bottom=None, top=None, emit=True, auto=False, **kwargs):
1187+
"""
1188+
See `~.polar.PolarAxes.set_ylim`.
1189+
"""
11861190
if 'rmin' in kwargs:
1187-
kwargs['ymin'] = kwargs.pop('rmin')
1191+
if bottom is None:
1192+
bottom = kwargs.pop('rmin')
1193+
else:
1194+
raise ValueError('Cannot supply both positional "bottom"'
1195+
'argument and kwarg "rmin"')
11881196
if 'rmax' in kwargs:
1189-
kwargs['ymax'] = kwargs.pop('rmax')
1190-
return self.set_ylim(*args, **kwargs)
1197+
if top is None:
1198+
top = kwargs.pop('rmax')
1199+
else:
1200+
raise ValueError('Cannot supply both positional "top"'
1201+
'argument and kwarg "rmax"')
1202+
return self.set_ylim(bottom=bottom, top=top, emit=emit, auto=auto,
1203+
**kwargs)
1204+
1205+
def set_ylim(self, bottom=None, top=None, emit=True, auto=False,
1206+
*, ymin=None, ymax=None):
1207+
"""
1208+
Set the data limits for the radial-axis.
1209+
1210+
Parameters
1211+
----------
1212+
bottom : scalar, optional
1213+
The bottom limit (default: None, which leaves the bottom
1214+
limit unchanged).
1215+
The bottom and top ylims may be passed as the tuple
1216+
(*bottom*, *top*) as the first positional argument (or as
1217+
the *bottom* keyword argument).
1218+
1219+
top : scalar, optional
1220+
The top ylim (default: None, which leaves the top limit
1221+
unchanged).
1222+
1223+
emit : bool, optional
1224+
Whether to notify observers of limit change (default: True).
1225+
1226+
auto : bool or None, optional
1227+
Whether to turn on autoscaling of the y-axis. True turns on,
1228+
False turns off (default action), None leaves unchanged.
1229+
1230+
ymin, ymax : scalar, optional
1231+
These arguments are deprecated and will be removed in a future
1232+
version. They are equivalent to bottom and top respectively,
1233+
and it is an error to pass both *ymin* and *bottom* or
1234+
*ymax* and *top*.
1235+
1236+
Returns
1237+
-------
1238+
ylimits : tuple
1239+
Returns the new y-axis limits as (*bottom*, *top*).
1240+
1241+
Notes
1242+
-----
1243+
The *bottom* value must be less than the *top* value, or a
1244+
ValueError is raised.
1245+
"""
1246+
1247+
if ymin is not None:
1248+
if bottom is not None:
1249+
raise ValueError('Cannot supply both positional "bottom" '
1250+
'argument and kwarg "ymin"')
1251+
else:
1252+
bottom = ymin
1253+
if ymax is not None:
1254+
if top is not None:
1255+
raise ValueError('Cannot supply both positional "top" '
1256+
'argument and kwarg "ymax"')
1257+
else:
1258+
top = ymax
1259+
if top is None and len(bottom) == 2:
1260+
top = bottom[1]
1261+
bottom = bottom[0]
1262+
if bottom >= top:
1263+
raise ValueError('Polar axes y/r-limits must be increasing; '
1264+
'i.e. bottom < top')
1265+
1266+
return super().set_ylim(bottom=bottom, top=top, emit=emit, auto=auto)
11911267

11921268
def get_rlabel_position(self):
11931269
"""

lib/matplotlib/tests/test_axes.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,15 @@ def test_polar_theta_position():
747747
ax.set_theta_direction('clockwise')
748748

749749

750+
def test_polar_invertedylim():
751+
fig = plt.figure()
752+
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True)
753+
with pytest.raises(ValueError):
754+
ax.set_ylim(90, -45)
755+
with pytest.raises(ValueError):
756+
ax.set_rlim(90, -45)
757+
758+
750759
@image_comparison(baseline_images=['polar_rlabel_position'], style='default')
751760
def test_polar_rlabel_position():
752761
fig = plt.figure()

0 commit comments

Comments
 (0)
0