matplotlib/matplotlib · error · TypeError
Incompatible X, Y inputs to {funcname}; see help({funcname})
Error message
Incompatible X, Y inputs to {funcname}; see help({funcname}) What it means
_pcolorargs (shared by pcolor/pcolormesh) broadcasts 1-D inputs to 2-D: X is repeated along rows to (Ny, Nx), Y along columns. After broadcasting, X.shape must equal Y.shape; a mismatch raises this TypeError pointing at help(funcname).
Source
Thrown at lib/matplotlib/axes/_axes.py:6516
if np.ma.is_masked(X) or np.ma.is_masked(Y):
raise ValueError(
'x and y arguments to pcolormesh cannot have '
'non-finite values or be of type '
'numpy.ma.MaskedArray with masked values')
nrows, ncols = C.shape[:2]
else:
raise _api.nargs_error(funcname, takes="1 or 3", given=len(args))
Nx = X.shape[-1]
Ny = Y.shape[0]
if X.ndim != 2 or X.shape[0] == 1:
x = X.reshape(1, Nx)
X = x.repeat(Ny, axis=0)
if Y.ndim != 2 or Y.shape[1] == 1:
y = Y.reshape(Ny, 1)
Y = y.repeat(Nx, axis=1)
if X.shape != Y.shape:
raise TypeError(f'Incompatible X, Y inputs to {funcname}; '
f'see help({funcname})')
if shading == 'auto':
if ncols == Nx and nrows == Ny:
shading = 'nearest'
else:
shading = 'flat'
if shading == 'flat':
if (Nx, Ny) != (ncols + 1, nrows + 1):
raise TypeError(f"Dimensions of C {C.shape} should"
f" be one smaller than X({Nx}) and Y({Ny})"
f" while using shading='flat'"
f" see help({funcname})")
else: # ['nearest', 'gouraud']:
if (Nx, Ny) != (ncols, nrows):
raise TypeError('Dimensions of C %s are incompatible with'
' X (%d) and/or Y (%d); see help(%s)' % (View on GitHub (pinned to b379c1b69e)
Solutions
- Generate both together: X, Y = np.meshgrid(x_1d, y_1d) - or just pass the 1-D vectors: ax.pcolormesh(x_1d, y_1d, C).
- Check X.shape == Y.shape right before plotting and transpose one (X = X.T) if a single axis is swapped.
- Remember the convention: X.shape[-1] is Nx (columns), Y.shape[0] is Ny (rows).
Example fix
// before X = np.meshgrid(xs, ys)[0] # (Ny, Nx) Y = other_ys # wrong length ax.pcolormesh(X, Y, C) // after X, Y = np.meshgrid(xs, ys) ax.pcolormesh(X, Y, C) # or ax.pcolormesh(xs, ys, C)
Defensive patterns
Strategy: validation
Validate before calling
import numpy as np
def xy_compatible(X, Y):
X, Y = np.asarray(X), np.asarray(Y)
Nx, Ny = X.shape[-1], Y.shape[0]
Xb = (np.repeat(X.reshape(1, Nx), Ny, axis=0)
if (X.ndim != 2 or X.shape[0] == 1) else X)
Yb = (np.repeat(Y.reshape(Ny, 1), Nx, axis=1)
if (Y.ndim != 2 or Y.shape[1] == 1) else Y)
return Xb.shape == Yb.shape
# usage: assert xy_compatible(X, Y) before pcolormesh/pcolor Prevention
- Always create X and Y from the same np.meshgrid call.
- Pass 1-D vectors (pcolormesh(x, y, C)) unless a warped grid requires 2-D.
- Assert X.shape == Y.shape in grid-loading code.
When it happens
Trigger: 2-D X of shape (Ny1, Nx) paired with 1-D y of length Ny2 != Ny1; X transposed to (Nx, Ny) relative to Y; X and Y taken from two different grids after regridding.
Common situations: np.meshgrid misuse (using only one output, or mixing sparse/dense); an old X grid kept with a new Y vector after a resolution change; assuming square grids make shape bugs invisible.
Related errors
- Dimensions of C %s are incompatible with X (%d) and/or Y (%d
- Matplotlib requires access to a writable cache directory, bu
- Key {key}: {ve}
- x and y arguments to pcolormesh cannot have non-finite value
- Dimensions of C {C.shape} should be one smaller than X({Nx})
AI-assisted analysis of matplotlib/matplotlib@b379c1b69e (2026-08-21).
Data as JSON: /api/errors/2f360b8400b3a2f7.
Report an issue: GitHub.