From 75cb8104b3844bb570a674bd24f72ebc87013b1e Mon Sep 17 00:00:00 2001 From: Greg Lucas Date: Wed, 2 Sep 2020 15:55:13 -0600 Subject: [PATCH] MAINT: Warn on non-increasing/decreasing pcolor coords pcolor* will now automatically calculate bin edges for users if shading is 'auto' or 'nearest'. This can lead to surprising resulting edge calculations if the coordinate arrays are not sorted prior to calling the function. --- 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 500ef4365092..ff03cc752a06 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -5682,6 +5682,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 a35f55a55db9..4b4775454d28 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1131,6 +1131,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"])