Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 75cb810

Browse files
committed
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.
1 parent f7ae157 commit 75cb810

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5682,6 +5682,15 @@ def _interp_grid(X):
56825682
# helper for below
56835683
if np.shape(X)[1] > 1:
56845684
dX = np.diff(X, axis=1)/2.
5685+
if not (np.all(dX >= 0) or np.all(dX <= 0)):
5686+
cbook._warn_external(
5687+
f"The input coordinates to {funcname} are "
5688+
"not all either increasing or decreasing, "
5689+
"the automatically computed edges can "
5690+
"produce misleading results in this case. "
5691+
"It is recommended to supply the "
5692+
f"quadrilateral edges to {funcname}"
5693+
f" yourself. See help({funcname}).")
56855694
X = np.hstack((X[:, [0]] - dX[:, [0]],
56865695
X[:, :-1] + dX,
56875696
X[:, [-1]] + dX[:, [-1]]))

lib/matplotlib/tests/test_axes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,13 @@ def test_pcolorargs():
11311131
x = np.ma.array(x, mask=(x < 0))
11321132
with pytest.raises(ValueError):
11331133
ax.pcolormesh(x, y, Z[:-1, :-1])
1134+
# Expect a warning with non-increasing coordinates
1135+
x = [359, 0, 1]
1136+
y = [-10, 10]
1137+
X, Y = np.meshgrid(x, y)
1138+
Z = np.zeros(X.shape)
1139+
with pytest.warns(UserWarning, match="The input coordinates"):
1140+
ax.pcolormesh(X, Y, Z, shading='auto')
11341141

11351142

11361143
@check_figures_equal(extensions=["png"])

0 commit comments

Comments
 (0)