From 257a6898aaeef656fa3405acf575962c3829b057 Mon Sep 17 00:00:00 2001 From: Ryan May Date: Mon, 14 Sep 2020 13:39:36 -0600 Subject: [PATCH] Backport PR #18398: Warn on non-increasing/decreasing pcolor coords --- lib/matplotlib/axes/_axes.py | 9 +++++++++ lib/matplotlib/tests/test_axes.py | 7 +++++++ 2 files changed, 16 insertions(+) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 5cc8c4a88280..696c51fabb91 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -5630,6 +5630,15 @@ def _interp_grid(X): # helper for below if np.shape(X)[1] > 1: dX = np.diff(X, axis=1)/2. + if not (np.all(dX >= 0) or np.all(dX <= 0)): + cbook._warn_external( + f"The input coordinates to {funcname} are " + "not all either increasing or decreasing, " + "the automatically computed edges can " + "produce misleading results in this case. " + "It is recommended to supply the " + f"quadrilateral edges to {funcname}" + f" yourself. See help({funcname}).") X = np.hstack((X[:, [0]] - dX[:, [0]], X[:, :-1] + dX, X[:, [-1]] + dX[:, [-1]])) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index d9e4726ac041..0f874f8cd806 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1162,6 +1162,13 @@ def test_pcolorargs(): x = np.ma.array(x, mask=(x < 0)) with pytest.raises(ValueError): ax.pcolormesh(x, y, Z[:-1, :-1]) + # Expect a warning with non-increasing coordinates + x = [359, 0, 1] + y = [-10, 10] + X, Y = np.meshgrid(x, y) + Z = np.zeros(X.shape) + with pytest.warns(UserWarning, match="The input coordinates"): + ax.pcolormesh(X, Y, Z, shading='auto') @check_figures_equal(extensions=["png"])