matplotlib/matplotlib · error · ValueError

X and Y must be the same size, but X.size is {X.size} and Y.

Error message

X and Y must be the same size, but X.size is {X.size} and Y.size is {Y.size}.

What it means

In Quiver/Barbs argument parsing (_parseargs), X and Y are raveled and must either form a grid matching U (len(X)==nc and len(Y)==nr, after which meshgrid expands them) or be equal-length position lists. When neither holds, matplotlib raises ValueError showing both offending sizes.

Source

Thrown at lib/matplotlib/quiver.py:484

        U, V = np.atleast_1d(*args)
    elif nargs == 3:
        U, V, C = np.atleast_1d(*args)
    elif nargs == 4:
        X, Y, U, V = np.atleast_1d(*args)
    elif nargs == 5:
        X, Y, U, V, C = np.atleast_1d(*args)
    else:
        raise _api.nargs_error(caller_name, takes="from 2 to 5", given=nargs)

    nr, nc = (1, U.shape[0]) if U.ndim == 1 else U.shape

    if X is not None:
        X = X.ravel()
        Y = Y.ravel()
        if len(X) == nc and len(Y) == nr:
            X, Y = (a.ravel() for a in np.meshgrid(X, Y))
        elif len(X) != len(Y):
            raise ValueError('X and Y must be the same size, but '
                             f'X.size is {X.size} and Y.size is {Y.size}.')
    else:
        indexgrid = np.meshgrid(np.arange(nc), np.arange(nr))
        X, Y = (np.ravel(a) for a in indexgrid)
    # Size validation for U, V, C is left to the set_UVC method.
    return X, Y, U, V, C


def _check_consistent_shapes(*arrays):
    all_shapes = {a.shape for a in arrays}
    if len(all_shapes) != 1:
        raise ValueError('The shapes of the passed in arrays do not match')


class Quiver(mcollections.PolyCollection):
    """
    Specialized PolyCollection for arrows.

View on GitHub (pinned to b379c1b69e)

Solutions

  1. Make positions equal length: len(x) == len(y) == u.ravel().size == v.ravel().size
  2. Or pass full 2-D grids: X, Y = np.meshgrid(x, y) with u, v of shape Y.shape
  3. Or drop X and Y entirely: plt.quiver(u, v) places arrows at integer index positions
  4. Pre-validate: x = np.asarray(x).ravel(); assert x.size == np.asarray(y).ravel().size

Example fix

# before
x = np.linspace(0, 1, 5)
y = np.linspace(0, 1, 3)
plt.quiver(x, y, u, v)  # ValueError: X.size is 5 and Y.size is 3

# after
X, Y = np.meshgrid(x, y)   # shapes match u, v of shape (3, 5)
plt.quiver(X, Y, u, v)
Defensive patterns

Strategy: validation

Validate before calling

import numpy as np
x, y, u = np.asarray(x), np.asarray(y), np.asarray(u)
nr, nc = (1, u.shape[0]) if u.ndim == 1 else u.shape
x, y = x.ravel(), y.ravel()
assert (len(x) == nc and len(y) == nr) or len(x) == len(y), 'X/Y sizes incompatible with U'
plt.quiver(x, y, u, v)

Try / catch

try:
    ax.quiver(x, y, u, v)
except ValueError as e:
    if 'X and Y must be the same size' in str(e):
        X, Y = np.meshgrid(np.ravel(x), np.ravel(y))
        ax.quiver(X, Y, u, v)
    else:
        raise

Prevention

When it happens

Trigger: plt.quiver(x, y, u, v) where x has 5 values and y has 3 while u.shape is (3, 5); passing a flattened X of length M*N with Y of length N; mixing meshgrid outputs with raw 1-D lists of different lengths.

Common situations: Station/vector plots built from unequal coordinate arrays; partially flattening grid coordinates; passing row coords and column coords of different lengths without meshgridding first.

Related errors


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