Skip to content

Commit 9edcdae

Browse files
timhoffmmeeseeksmachine
authored andcommitted
Backport PR matplotlib#29213: avoid-unnecessary-warning-in-_pcolorargs-function
1 parent ee02ba7 commit 9edcdae

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5979,11 +5979,15 @@ def _pcolorargs(self, funcname, *args, shading='auto', **kwargs):
59795979
# grid is specified at the center, so define corners
59805980
# at the midpoints between the grid centers and then use the
59815981
# flat algorithm.
5982-
def _interp_grid(X):
5983-
# helper for below
5982+
def _interp_grid(X, require_monotonicity=False):
5983+
# helper for below. To ensure the cell edges are calculated
5984+
# correctly, when expanding columns, the monotonicity of
5985+
# X coords needs to be checked. When expanding rows, the
5986+
# monotonicity of Y coords needs to be checked.
59845987
if np.shape(X)[1] > 1:
59855988
dX = np.diff(X, axis=1) * 0.5
5986-
if not (np.all(dX >= 0) or np.all(dX <= 0)):
5989+
if (require_monotonicity and
5990+
not (np.all(dX >= 0) or np.all(dX <= 0))):
59875991
_api.warn_external(
59885992
f"The input coordinates to {funcname} are "
59895993
"interpreted as cell centers, but are not "
@@ -6003,11 +6007,11 @@ def _interp_grid(X):
60036007
return X
60046008

60056009
if ncols == Nx:
6006-
X = _interp_grid(X)
6010+
X = _interp_grid(X, require_monotonicity=True)
60076011
Y = _interp_grid(Y)
60086012
if nrows == Ny:
60096013
X = _interp_grid(X.T).T
6010-
Y = _interp_grid(Y.T).T
6014+
Y = _interp_grid(Y.T, require_monotonicity=True).T
60116015
shading = 'flat'
60126016

60136017
C = cbook.safe_masked_invalid(C, copy=True)

lib/matplotlib/tests/test_axes.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1589,14 +1589,37 @@ def test_pcolorargs():
15891589
x = np.ma.array(x, mask=(x < 0))
15901590
with pytest.raises(ValueError):
15911591
ax.pcolormesh(x, y, Z[:-1, :-1])
1592-
# Expect a warning with non-increasing coordinates
1592+
# If the X or Y coords do not possess monotonicity in their respective
1593+
# directions, a warning indicating a bad grid will be triggered.
1594+
# The case of specifying coordinates by inputting 1D arrays.
15931595
x = [359, 0, 1]
15941596
y = [-10, 10]
15951597
X, Y = np.meshgrid(x, y)
15961598
Z = np.zeros(X.shape)
15971599
with pytest.warns(UserWarning,
15981600
match='are not monotonically increasing or decreasing'):
15991601
ax.pcolormesh(X, Y, Z, shading='auto')
1602+
# The case of specifying coordinates by inputting 2D arrays.
1603+
x = np.linspace(-1, 1, 3)
1604+
y = np.linspace(-1, 1, 3)
1605+
X, Y = np.meshgrid(x, y)
1606+
Z = np.zeros(X.shape)
1607+
np.random.seed(19680801)
1608+
noise_X = np.random.random(X.shape)
1609+
noise_Y = np.random.random(Y.shape)
1610+
with pytest.warns(UserWarning,
1611+
match='are not monotonically increasing or '
1612+
'decreasing') as record:
1613+
# Small perturbations in coordinates will not disrupt the monotonicity
1614+
# of the X-coords and Y-coords in their respective directions.
1615+
# Therefore, no warnings will be triggered.
1616+
ax.pcolormesh(X+noise_X, Y+noise_Y, Z, shading='auto')
1617+
assert len(record) == 0
1618+
# Large perturbations have disrupted the monotonicity of the X-coords
1619+
# and Y-coords in their respective directions, thus resulting in two
1620+
# bad grid warnings.
1621+
ax.pcolormesh(X+10*noise_X, Y+10*noise_Y, Z, shading='auto')
1622+
assert len(record) == 2
16001623

16011624

16021625
def test_pcolormesh_underflow_error():

0 commit comments

Comments
 (0)