Skip to content

Commit

Permalink
Backport PR matplotlib#29213: avoid-unnecessary-warning-in-_pcolorarg…
Browse files Browse the repository at this point in the history
…s-function
  • Loading branch information
timhoffm authored and meeseeksmachine committed Dec 4, 2024
1 parent ee02ba7 commit 9edcdae
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
14 changes: 9 additions & 5 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5979,11 +5979,15 @@ def _pcolorargs(self, funcname, *args, shading='auto', **kwargs):
# grid is specified at the center, so define corners
# at the midpoints between the grid centers and then use the
# flat algorithm.
def _interp_grid(X):
# helper for below
def _interp_grid(X, require_monotonicity=False):
# helper for below. To ensure the cell edges are calculated
# correctly, when expanding columns, the monotonicity of
# X coords needs to be checked. When expanding rows, the
# monotonicity of Y coords needs to be checked.
if np.shape(X)[1] > 1:
dX = np.diff(X, axis=1) * 0.5
if not (np.all(dX >= 0) or np.all(dX <= 0)):
if (require_monotonicity and
not (np.all(dX >= 0) or np.all(dX <= 0))):
_api.warn_external(
f"The input coordinates to {funcname} are "
"interpreted as cell centers, but are not "
Expand All @@ -6003,11 +6007,11 @@ def _interp_grid(X):
return X

if ncols == Nx:
X = _interp_grid(X)
X = _interp_grid(X, require_monotonicity=True)
Y = _interp_grid(Y)
if nrows == Ny:
X = _interp_grid(X.T).T
Y = _interp_grid(Y.T).T
Y = _interp_grid(Y.T, require_monotonicity=True).T
shading = 'flat'

C = cbook.safe_masked_invalid(C, copy=True)
Expand Down
25 changes: 24 additions & 1 deletion lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1589,14 +1589,37 @@ 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
# If the X or Y coords do not possess monotonicity in their respective
# directions, a warning indicating a bad grid will be triggered.
# The case of specifying coordinates by inputting 1D arrays.
x = [359, 0, 1]
y = [-10, 10]
X, Y = np.meshgrid(x, y)
Z = np.zeros(X.shape)
with pytest.warns(UserWarning,
match='are not monotonically increasing or decreasing'):
ax.pcolormesh(X, Y, Z, shading='auto')
# The case of specifying coordinates by inputting 2D arrays.
x = np.linspace(-1, 1, 3)
y = np.linspace(-1, 1, 3)
X, Y = np.meshgrid(x, y)
Z = np.zeros(X.shape)
np.random.seed(19680801)
noise_X = np.random.random(X.shape)
noise_Y = np.random.random(Y.shape)
with pytest.warns(UserWarning,
match='are not monotonically increasing or '
'decreasing') as record:
# Small perturbations in coordinates will not disrupt the monotonicity
# of the X-coords and Y-coords in their respective directions.
# Therefore, no warnings will be triggered.
ax.pcolormesh(X+noise_X, Y+noise_Y, Z, shading='auto')
assert len(record) == 0
# Large perturbations have disrupted the monotonicity of the X-coords
# and Y-coords in their respective directions, thus resulting in two
# bad grid warnings.
ax.pcolormesh(X+10*noise_X, Y+10*noise_Y, Z, shading='auto')
assert len(record) == 2


def test_pcolormesh_underflow_error():
Expand Down

0 comments on commit 9edcdae

Please sign in to comment.