matplotlib/matplotlib · error · ValueError

The rows of 'x' must be equal

Error message

The rows of 'x' must be equal

What it means

streamplot's Grid accepts 1D coordinate arrays, or 2D arrays equivalent to np.meshgrid(x_1d, y_1d): every row of x must be identical (and every column of y). If the rows of a 2D x differ, the coordinates do not describe a regular Cartesian grid and this ValueError is raised. The near-universal cause is a meshgrid built with indexing='ij', or x and y passed in swapped order.

Source

Thrown at lib/matplotlib/streamplot.py:378

        if not self.grid.within_grid(xg, yg):
            raise InvalidIndexError
        xm, ym = self.grid2mask(xg, yg)
        self.mask._update_trajectory(xm, ym, broken_streamlines)

    def undo_trajectory(self):
        self.mask._undo_trajectory()


class Grid:
    """Grid of data."""
    def __init__(self, x, y):

        if np.ndim(x) == 1:
            pass
        elif np.ndim(x) == 2:
            x_row = x[0]
            if not np.allclose(x_row, x):
                raise ValueError("The rows of 'x' must be equal")
            x = x_row
        else:
            raise ValueError("'x' can have at maximum 2 dimensions")

        if np.ndim(y) == 1:
            pass
        elif np.ndim(y) == 2:
            yt = np.transpose(y)  # Also works for nested lists.
            y_col = yt[0]
            if not np.allclose(y_col, yt):
                raise ValueError("The columns of 'y' must be equal")
            y = y_col
        else:
            raise ValueError("'y' can have at maximum 2 dimensions")

        if not (np.diff(x) > 0).all():
            raise ValueError("'x' must be strictly increasing")
        if not (np.diff(y) > 0).all():

View on GitHub (pinned to b379c1b69e)

Solutions

  1. Pass 1D coordinate arrays: ax.streamplot(x_1d, y_1d, u, v)
  2. For 2D coordinates use default indexing: X, Y = np.meshgrid(x, y), whose rows of X are constant
  3. For 'ij'-shaped data, transpose the 2D coordinate arrays and u, v before calling

Example fix

# before
X, Y = np.meshgrid(x, y, indexing='ij')
ax.streamplot(X, Y, u, v)  # ValueError: rows of 'x' not equal

# after
ax.streamplot(x, y, u.T, v.T)  # 1D coords, 'xy' convention
Defensive patterns

Strategy: validation

Validate before calling

x, y = np.asarray(x), np.asarray(y)
if x.ndim == 2 and not np.allclose(x, x[0]):
    x, y, u, v = x.T, y.T, u.T, v.T  # input was 'ij'-gridded
ax.streamplot(x, y, u, v)

Type guard

def is_xy_meshgrid(x) -> bool:
    x = np.asarray(x)
    return x.ndim == 1 or np.allclose(x, x[0])

Prevention

When it happens

Trigger: X, Y = np.meshgrid(x, y, indexing='ij') where rows of X vary; passing np.meshgrid(y, x) output in the wrong order; feeding curvilinear coordinates from unstructured grids.

Common situations: Numerical codes written with matrix (row-major) indexing; porting plotting code between matplotlib and libraries that assume 'ij'-shaped grids.

Related errors


AI-assisted analysis of matplotlib/matplotlib@b379c1b69e (2026-08-21). Data as JSON: /api/errors/88e3a8633f545111. Report an issue: GitHub.