Visitar URL original
FIX: Raise for single row/column grids with nearest shading · matplotlib/matplotlib@5f8c2f0 · GitHub
Skip to content

Commit 5f8c2f0

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 5f8c2f0

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

‎lib/matplotlib/axes/_axes.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6560,8 +6560,14 @@ def _interp_grid(X, require_monotonicity=False):
65606560
X[:, :-1] + dX,
65616561
X[:, [-1]] + dX[:, [-1]]))
65626562
else:
6563-
# This is just degenerate, but we can't reliably guess
6564-
# a dX if there is just one value.
6563+
if require_monotonicity:
6564+
# Coordinate array has a single cell center, so its
6565+
# corresponding cell size cannot be determined
6566+
# from neighboring centers.
6567+
raise TypeError(f"Dimensions of X ({Nx}) and Y ({Ny}) "
6568+
"must be greater than one when using "
6569+
f"shading='nearest'. Supply explicit "
6570+
f"cell edges to {funcname}.")
65656571
X = np.hstack((X, X))
65666572
return X
65676573

‎lib/matplotlib/tests/test_axes.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,6 +1678,16 @@ 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(x, y, Z)
1687+
with pytest.raises(TypeError):
1688+
ax.pcolormesh(y, x, Z.T)
1689+
with pytest.raises(TypeError):
1690+
ax.pcolormesh(y, y, Z[:, [0]])
16811691

16821692

16831693
def test_pcolormesh_underflow_error():

0 commit comments

Comments
 (0)