Skip to content

Commit beb341e

Browse files
committed
FIX: Raise for single row/column grids with nearest shading
Explicitly providing a single row or single column grid for pcolor(mesh) with nearest shading, silently produces no plot. This happens because nearest shading interprets the X and Y coordinates as cell centers and then calculates, from neighboring centers, new X and Y coordinates for the cell corners. When a coordinate array contains only one center, its corresponding cell size cannot be determined from neighboring centers. This commit fixes the behavior of silently producing no plot by raising an error for such grids and recommending that users supply cell corners instead.
1 parent 2d0352a commit beb341e

2 files changed

Lines changed: 36 additions & 20 deletions

File tree

‎lib/matplotlib/axes/_axes.py‎

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6535,6 +6535,15 @@ def _pcolorargs(self, funcname, *args, shading='auto', **kwargs):
65356535
' X (%d) and/or Y (%d); see help(%s)' % (
65366536
C.shape, Nx, Ny, funcname))
65376537
if shading == 'nearest':
6538+
if Nx == 1 or Ny == 1:
6539+
# If a coordinate array has a single cell center,
6540+
# its corresponding cell size cannot be determined
6541+
# from neighboring centers.
6542+
raise TypeError(f"C has shape ({nrows}, {ncols}) but "
6543+
"shading='nearest' requires at least two rows "
6544+
"and two columns. Please supply explicit cell "
6545+
"edges for X and Y and use shading='flat' "
6546+
f"with {funcname}.")
65386547
# grid is specified at the center, so define corners
65396548
# at the midpoints between the grid centers and then use the
65406549
# flat algorithm.
@@ -6543,26 +6552,21 @@ def _interp_grid(X, require_monotonicity=False):
65436552
# correctly, when expanding columns, the monotonicity of
65446553
# X coords needs to be checked. When expanding rows, the
65456554
# monotonicity of Y coords needs to be checked.
6546-
if np.shape(X)[1] > 1:
6547-
dX = np.diff(X, axis=1) * 0.5
6548-
if (require_monotonicity and
6549-
not (np.all(dX >= 0) or np.all(dX <= 0))):
6550-
_api.warn_external(
6551-
f"The input coordinates to {funcname} are "
6552-
"interpreted as cell centers, but are not "
6553-
"monotonically increasing or decreasing. "
6554-
"This may lead to incorrectly calculated cell "
6555-
"edges, in which case, please supply "
6556-
f"explicit cell edges to {funcname}.")
6557-
6558-
hstack = np.ma.hstack if np.ma.isMA(X) else np.hstack
6559-
X = hstack((X[:, [0]] - dX[:, [0]],
6560-
X[:, :-1] + dX,
6561-
X[:, [-1]] + dX[:, [-1]]))
6562-
else:
6563-
# This is just degenerate, but we can't reliably guess
6564-
# a dX if there is just one value.
6565-
X = np.hstack((X, X))
6555+
dX = np.diff(X, axis=1) * 0.5
6556+
if (require_monotonicity and
6557+
not (np.all(dX >= 0) or np.all(dX <= 0))):
6558+
_api.warn_external(
6559+
f"The input coordinates to {funcname} are "
6560+
"interpreted as cell centers, but are not "
6561+
"monotonically increasing or decreasing. "
6562+
"This may lead to incorrectly calculated cell "
6563+
"edges, in which case, please supply "
6564+
f"explicit cell edges to {funcname}.")
6565+
6566+
hstack = np.ma.hstack if np.ma.isMA(X) else np.hstack
6567+
X = hstack((X[:, [0]] - dX[:, [0]],
6568+
X[:, :-1] + dX,
6569+
X[:, [-1]] + dX[:, [-1]]))
65666570
return X
65676571

65686572
if ncols == Nx:

‎lib/matplotlib/tests/test_axes.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,6 +1678,18 @@ def test_pcolorargs():
16781678
# bad grid warnings.
16791679
ax.pcolormesh(X+10*noise_X, Y+10*noise_Y, Z, shading='auto')
16801680
assert len(record) == 2
1681+
# Grid cannot contain a single row or single column for nearest shading.
1682+
x = [1, 2, 3]
1683+
y = [4]
1684+
Z = np.arange(3).reshape(1, 3)
1685+
with pytest.raises(TypeError):
1686+
ax.pcolormesh(Z, shading='nearest')
1687+
with pytest.raises(TypeError):
1688+
ax.pcolormesh(x, y, Z)
1689+
with pytest.raises(TypeError):
1690+
ax.pcolormesh(y, x, Z.T)
1691+
with pytest.raises(TypeError):
1692+
ax.pcolormesh(y, y, Z[:, [0]])
16811693

16821694

16831695
def test_pcolormesh_underflow_error():

0 commit comments

Comments
 (0)