From 757282354c88eb25610a2ca60a4dda88f2053fda Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Tue, 1 Jul 2025 03:47:19 -0400 Subject: [PATCH] Check that stem input is 1D This also has the side-effect of casting torch Tensors to NumPy arrays, which fixes #30216. Since `stem` is made up of `plot` and `[hv]lines` whic already do this cast, this just moves it up one level which prevents doing it twice. --- lib/matplotlib/axes/_axes.py | 3 +++ lib/matplotlib/tests/test_axes.py | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index b4ed7ae22d35..8ac300296538 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -3436,6 +3436,9 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0, else: # horizontal heads, locs = self._process_unit_info([("x", heads), ("y", locs)]) + heads = cbook._check_1d(heads) + locs = cbook._check_1d(locs) + # resolve line format if linefmt is None: linefmt = args[0] if len(args) > 0 else "C0-" diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index db0629de99b5..bba9f8648799 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -4744,6 +4744,11 @@ def _assert_equal(stem_container, expected): _assert_equal(ax.stem(y, linefmt='r--'), expected=([0, 1, 2], y)) _assert_equal(ax.stem(y, 'r--'), expected=([0, 1, 2], y)) + with pytest.raises(ValueError): + ax.stem([[y]]) + with pytest.raises(ValueError): + ax.stem([[x]], y) + def test_stem_markerfmt(): """Test that stem(..., markerfmt=...) produces the intended markers."""