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

Skip to content

Commit ff67864

Browse files
authored
Merge pull request #11271 from Zac-HD/refactor
Better argspecs for Axes.stem
2 parents 1e6790f + 51a93f0 commit ff67864

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Consistent handling of \*args in Axes.stem
2+
------------------------------------------
3+
4+
:meth:`matplotlib.axex.Axes.stem` now raises TypeError when passed
5+
unhandled positional arguments. If two or more arguments are passed
6+
(ie X, Y, [linefmt], ...) and Y cannot be cast to an array, an error
7+
will be raised instead of treating X as Y and Y as linefmt.

lib/matplotlib/axes/_axes.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2350,17 +2350,19 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None,
23502350
which inspired this method.
23512351
23522352
"""
2353-
# Assume there's at least one data array
2353+
if not 1 <= len(args) <= 5:
2354+
raise TypeError('stem expected between 1 and 5 positional '
2355+
'arguments, got {}'.format(args))
2356+
23542357
y = np.asarray(args[0])
23552358
args = args[1:]
23562359

23572360
# Try a second one
2358-
try:
2359-
x, y = y, np.asarray(args[0], dtype=float)
2360-
except (IndexError, ValueError):
2361-
# The second array doesn't make sense, or it doesn't exist
2361+
if not args:
23622362
x = np.arange(len(y))
23632363
else:
2364+
x = y
2365+
y = np.asarray(args[0], dtype=float)
23642366
args = args[1:]
23652367

23662368
# defaults for formats

lib/matplotlib/tests/test_mlab.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,7 @@ def test_detrend_detrend_linear_1d_slope_off_axis1(self):
11191119
res = mlab.detrend(input, key=mlab.detrend_linear, axis=0)
11201120
assert_allclose(res, targ, atol=self.atol)
11211121

1122-
def test_detrend_str_linear_2d_slope_off_axis0(self):
1122+
def test_detrend_str_linear_2d_slope_off_axis0_notranspose(self):
11231123
arri = [self.sig_off,
11241124
self.sig_slope,
11251125
self.sig_slope + self.sig_off]
@@ -1131,7 +1131,7 @@ def test_detrend_str_linear_2d_slope_off_axis0(self):
11311131
res = mlab.detrend(input, key='linear', axis=1)
11321132
assert_allclose(res, targ, atol=self.atol)
11331133

1134-
def test_detrend_detrend_linear_1d_slope_off_axis1(self):
1134+
def test_detrend_detrend_linear_1d_slope_off_axis1_notranspose(self):
11351135
arri = [self.sig_off,
11361136
self.sig_slope,
11371137
self.sig_slope + self.sig_off]

0 commit comments

Comments
 (0)