Skip to content

Commit 7514890

Browse files
authored
Merge pull request matplotlib#29225 from meeseeksmachine/auto-backport-of-pr-29213-on-v3.10.x
Backport PR matplotlib#29213 on branch v3.10.x (avoid-unnecessary-warning-in-_pcolorargs-function)
2 parents 9567ac1 + 9edcdae commit 7514890

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
@@ -6032,11 +6032,15 @@ def _pcolorargs(self, funcname, *args, shading='auto', **kwargs):
60326032
# grid is specified at the center, so define corners
60336033
# at the midpoints between the grid centers and then use the
60346034
# flat algorithm.
6035-
def _interp_grid(X):
6036-
# helper for below
6035+
def _interp_grid(X, require_monotonicity=False):
6036+
# helper for below. To ensure the cell edges are calculated
6037+
# correctly, when expanding columns, the monotonicity of
6038+
# X coords needs to be checked. When expanding rows, the
6039+
# monotonicity of Y coords needs to be checked.
60376040
if np.shape(X)[1] > 1:
60386041
dX = np.diff(X, axis=1) * 0.5
6039-
if not (np.all(dX >= 0) or np.all(dX <= 0)):
6042+
if (require_monotonicity and
6043+
not (np.all(dX >= 0) or np.all(dX <= 0))):
60406044
_api.warn_external(
60416045
f"The input coordinates to {funcname} are "
60426046
"interpreted as cell centers, but are not "
@@ -6056,11 +6060,11 @@ def _interp_grid(X):
60566060
return X
60576061

60586062
if ncols == Nx:
6059-
X = _interp_grid(X)
6063+
X = _interp_grid(X, require_monotonicity=True)
60606064
Y = _interp_grid(Y)
60616065
if nrows == Ny:
60626066
X = _interp_grid(X.T).T
6063-
Y = _interp_grid(Y.T).T
6067+
Y = _interp_grid(Y.T, require_monotonicity=True).T
60646068
shading = 'flat'
60656069

60666070
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)