Visitar URL original
FIX: Support single row/column grids for nearest shading · matplotlib/matplotlib@4153147 · GitHub
Skip to content

Commit 4153147

Browse files
committed
FIX: Support single row/column grids for nearest shading
Explicitly providing a single row and/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 adds a fallback that determines the directional cell size for a single center by taking half of its absolute value on either side. For a zero-valued center -/+ 0.5 is used instead. This allows the cells to be plotted. And a warning is emitted to notify the user.
1 parent 2d0352a commit 4153147

2 files changed

Lines changed: 31 additions & 3 deletions

File tree

‎lib/matplotlib/axes/_axes.py‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6560,9 +6560,23 @@ 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.
6565-
X = np.hstack((X, X))
6563+
if require_monotonicity:
6564+
# Fallback if coordinate array has only one cell center
6565+
# so its corresponding cell size cannot be determined
6566+
# from neighboring centers.
6567+
_api.warn_external(
6568+
"A coordinate array contains a single cell center "
6569+
f"along one axis, so {funcname} cannot determine "
6570+
"a cell size in that direction to calculate the "
6571+
"cell edges. A default cell size is being used; "
6572+
f"supply explicit cell edges to {funcname} instead "
6573+
"to avoid this warning.")
6574+
dX = np.abs(X) * 0.5
6575+
dX[dX == 0] = 0.5
6576+
hstack = np.ma.hstack if np.ma.isMA(X) else np.hstack
6577+
X = hstack((X - dX, X + dX))
6578+
else:
6579+
X = np.hstack((X, X))
65666580
return X
65676581

65686582
if ncols == Nx:

‎lib/matplotlib/tests/test_axes.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,6 +1678,20 @@ 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+
# If the grid contains a single row and/or column for nearest shading,
1682+
# a warning is emitted and a default cell size is used.
1683+
x = [2, 4, 6]
1684+
y = [5]
1685+
Z = np.arange(3).reshape(1, 3)
1686+
with pytest.warns(UserWarning,
1687+
match='coordinate array contains a single cell center'):
1688+
ax.pcolormesh(x, y, Z)
1689+
with pytest.warns(UserWarning,
1690+
match='coordinate array contains a single cell center'):
1691+
ax.pcolormesh(y, x, Z.T)
1692+
with pytest.warns(UserWarning,
1693+
match='coordinate array contains a single cell center'):
1694+
ax.pcolormesh(y, y, Z[:, [0]])
16811695

16821696

16831697
def test_pcolormesh_underflow_error():

0 commit comments

Comments
 (0)