From de80f58b780f0dc700fd353be8fdb73b8769680e Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Fri, 20 Sep 2024 15:17:32 +0200 Subject: [PATCH 1/2] MNT: Check the input sizes of regular X,Y in pcolorfast Closes #28059. --- lib/matplotlib/axes/_axes.py | 9 +++++++++ lib/matplotlib/tests/test_axes.py | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index d7b649ae437f..6824006e7bdf 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -6634,6 +6634,15 @@ def pcolorfast(self, *args, alpha=None, norm=None, cmap=None, vmin=None, if x.size == 2 and y.size == 2: style = "image" else: + if x.size != nc + 1: + raise ValueError( + f"Length of X ({x.size}) must be one larger than the " + f"number of columns in C ({nc})") + if y.size != nr + 1: + raise ValueError( + f"Length of Y ({y.size}) must be one larger than the " + f"number of rows in C ({nr})" + ) dx = np.diff(x) dy = np.diff(y) if (np.ptp(dx) < 0.01 * abs(dx.mean()) and diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 33c81c44abaf..2ee2339405c8 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -6626,6 +6626,27 @@ def test_pcolorfast_bad_dims(): ax.pcolorfast(np.empty(6), np.empty((4, 7)), np.empty((8, 8))) +def test_pcolorfast_regular_xy_incompatible_size(): + """ + Test that the sizes of X, Y, C are compatible for regularly spaced X, Y. + + Note that after the regualar-spacing check, pcolorfast may go into the + fast "image" mode, where the individual X, Y positions are not used anymore. + Therefore, the algorithm had worked with any regularly number of regularly + spaced values, but discarded their values. + """ + fig, ax = plt.subplots() + with pytest.raises( + ValueError, match="Length of X (5) must be one larger than the " + "number of columns in C (20)"): + ax.pcolorfast(np.arange(5), np.arange(11), np.random.rand(10, 20)) + + with pytest.raises( + ValueError, match="Length of Y (5) must be one larger than the " + "number rows in C (10)"): + ax.pcolorfast(np.arange(21), np.arange(5), np.random.rand(10, 20)) + + def test_shared_scale(): fig, axs = plt.subplots(2, 2, sharex=True, sharey=True) From bc648cae0f64f6f56f352ae1383c0a0cc805fae9 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Sun, 22 Sep 2024 00:51:33 +0200 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Elliott Sales de Andrade --- lib/matplotlib/tests/test_axes.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 2ee2339405c8..cbed05fb2f1d 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -6637,13 +6637,13 @@ def test_pcolorfast_regular_xy_incompatible_size(): """ fig, ax = plt.subplots() with pytest.raises( - ValueError, match="Length of X (5) must be one larger than the " - "number of columns in C (20)"): + ValueError, match=r"Length of X \(5\) must be one larger than the " + r"number of columns in C \(20\)"): ax.pcolorfast(np.arange(5), np.arange(11), np.random.rand(10, 20)) with pytest.raises( - ValueError, match="Length of Y (5) must be one larger than the " - "number rows in C (10)"): + ValueError, match=r"Length of Y \(5\) must be one larger than the " + r"number of rows in C \(10\)"): ax.pcolorfast(np.arange(21), np.arange(5), np.random.rand(10, 20))