Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 24 additions & 20 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6535,6 +6535,15 @@ def _pcolorargs(self, funcname, *args, shading='auto', **kwargs):
' X (%d) and/or Y (%d); see help(%s)' % (
C.shape, Nx, Ny, funcname))
if shading == 'nearest':
if Nx == 1 or Ny == 1:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the error message could be confusing between number of dimensions and the length of the appropriate dimension. I think you need to refer to the size of the data array not X and Y.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that it may be confusing. I pushed a new commit to make the message clearer and refer to the data array.

# If a coordinate array has a single cell center,
# its corresponding cell size cannot be determined
# from neighboring centers.
raise TypeError(f"C has shape ({nrows}, {ncols}) but "
"shading='nearest' requires at least two rows "
"and two columns. Please supply explicit cell "
"edges for X and Y and use shading='flat' "
f"with {funcname}.")
# grid is specified at the center, so define corners
# at the midpoints between the grid centers and then use the
# flat algorithm.
Expand All @@ -6543,26 +6552,21 @@ def _interp_grid(X, require_monotonicity=False):
# correctly, when expanding columns, the monotonicity of
# X coords needs to be checked. When expanding rows, the
# monotonicity of Y coords needs to be checked.
if np.shape(X)[1] > 1:
dX = np.diff(X, axis=1) * 0.5
if (require_monotonicity and
not (np.all(dX >= 0) or np.all(dX <= 0))):
_api.warn_external(
f"The input coordinates to {funcname} are "
"interpreted as cell centers, but are not "
"monotonically increasing or decreasing. "
"This may lead to incorrectly calculated cell "
"edges, in which case, please supply "
f"explicit cell edges to {funcname}.")

hstack = np.ma.hstack if np.ma.isMA(X) else np.hstack
X = hstack((X[:, [0]] - dX[:, [0]],
X[:, :-1] + dX,
X[:, [-1]] + dX[:, [-1]]))
else:
# This is just degenerate, but we can't reliably guess
# a dX if there is just one value.
X = np.hstack((X, X))
dX = np.diff(X, axis=1) * 0.5
if (require_monotonicity and
not (np.all(dX >= 0) or np.all(dX <= 0))):
_api.warn_external(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not following why this had to change

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the new check for single row or single column grids with nearest shading, interp_grid() is guaranteed to handle coordinate arrays with at least two columns (therefore np.shape(X)[1] is always greater than one).

So it made sense to me to remove the else branch entirely (which handles arrays with one column) and remove the first conditional which will always be true.

f"The input coordinates to {funcname} are "
"interpreted as cell centers, but are not "
"monotonically increasing or decreasing. "
"This may lead to incorrectly calculated cell "
"edges, in which case, please supply "
f"explicit cell edges to {funcname}.")

hstack = np.ma.hstack if np.ma.isMA(X) else np.hstack
X = hstack((X[:, [0]] - dX[:, [0]],
X[:, :-1] + dX,
X[:, [-1]] + dX[:, [-1]]))
return X

if ncols == Nx:
Expand Down
12 changes: 12 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1678,6 +1678,18 @@ def test_pcolorargs():
# bad grid warnings.
ax.pcolormesh(X+10*noise_X, Y+10*noise_Y, Z, shading='auto')
assert len(record) == 2
# Grid cannot contain a single row or single column for nearest shading.
x = [1, 2, 3]
y = [4]
Z = np.arange(3).reshape(1, 3)
with pytest.raises(TypeError):
ax.pcolormesh(Z, shading='nearest')
with pytest.raises(TypeError):
ax.pcolormesh(x, y, Z)
with pytest.raises(TypeError):
ax.pcolormesh(y, x, Z.T)
with pytest.raises(TypeError):
ax.pcolormesh(y, y, Z[:, [0]])


def test_pcolormesh_underflow_error():
Expand Down
Loading