diff --git a/doc/api/next_api_changes/2018-05-06-ZHD.rst b/doc/api/next_api_changes/2018-05-06-ZHD.rst new file mode 100644 index 000000000000..61a8886c4890 --- /dev/null +++ b/doc/api/next_api_changes/2018-05-06-ZHD.rst @@ -0,0 +1,7 @@ +Consistent handling of \*args in Axes.stem +------------------------------------------ + +:meth:`matplotlib.axex.Axes.stem` now raises TypeError when passed +unhandled positional arguments. If two or more arguments are passed +(ie X, Y, [linefmt], ...) and Y cannot be cast to an array, an error +will be raised instead of treating X as Y and Y as linefmt. diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index d6e4f50a2d7e..4e7551872882 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -2350,17 +2350,19 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, which inspired this method. """ - # Assume there's at least one data array + if not 1 <= len(args) <= 5: + raise TypeError('stem expected between 1 and 5 positional ' + 'arguments, got {}'.format(args)) + y = np.asarray(args[0]) args = args[1:] # Try a second one - try: - x, y = y, np.asarray(args[0], dtype=float) - except (IndexError, ValueError): - # The second array doesn't make sense, or it doesn't exist + if not args: x = np.arange(len(y)) else: + x = y + y = np.asarray(args[0], dtype=float) args = args[1:] # defaults for formats diff --git a/lib/matplotlib/tests/test_mlab.py b/lib/matplotlib/tests/test_mlab.py index d336fea01ff7..cd2c54b71b25 100644 --- a/lib/matplotlib/tests/test_mlab.py +++ b/lib/matplotlib/tests/test_mlab.py @@ -1119,7 +1119,7 @@ def test_detrend_detrend_linear_1d_slope_off_axis1(self): res = mlab.detrend(input, key=mlab.detrend_linear, axis=0) assert_allclose(res, targ, atol=self.atol) - def test_detrend_str_linear_2d_slope_off_axis0(self): + def test_detrend_str_linear_2d_slope_off_axis0_notranspose(self): arri = [self.sig_off, self.sig_slope, self.sig_slope + self.sig_off] @@ -1131,7 +1131,7 @@ def test_detrend_str_linear_2d_slope_off_axis0(self): res = mlab.detrend(input, key='linear', axis=1) assert_allclose(res, targ, atol=self.atol) - def test_detrend_detrend_linear_1d_slope_off_axis1(self): + def test_detrend_detrend_linear_1d_slope_off_axis1_notranspose(self): arri = [self.sig_off, self.sig_slope, self.sig_slope + self.sig_off]