{"record":{"id":"1aace14ea7e00eee","repo":"matplotlib/matplotlib","slug":"x-and-y-must-be-the-same-size-but-x-size-is-x-si","errorCode":null,"errorMessage":"X and Y must be the same size, but X.size is {X.size} and Y.size is {Y.size}.","messagePattern":"X and Y must be the same size, but X\\.size is (.+?) and Y\\.size is (.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"lib/matplotlib/quiver.py","lineNumber":484,"sourceCode":"        U, V = np.atleast_1d(*args)\n    elif nargs == 3:\n        U, V, C = np.atleast_1d(*args)\n    elif nargs == 4:\n        X, Y, U, V = np.atleast_1d(*args)\n    elif nargs == 5:\n        X, Y, U, V, C = np.atleast_1d(*args)\n    else:\n        raise _api.nargs_error(caller_name, takes=\"from 2 to 5\", given=nargs)\n\n    nr, nc = (1, U.shape[0]) if U.ndim == 1 else U.shape\n\n    if X is not None:\n        X = X.ravel()\n        Y = Y.ravel()\n        if len(X) == nc and len(Y) == nr:\n            X, Y = (a.ravel() for a in np.meshgrid(X, Y))\n        elif len(X) != len(Y):\n            raise ValueError('X and Y must be the same size, but '\n                             f'X.size is {X.size} and Y.size is {Y.size}.')\n    else:\n        indexgrid = np.meshgrid(np.arange(nc), np.arange(nr))\n        X, Y = (np.ravel(a) for a in indexgrid)\n    # Size validation for U, V, C is left to the set_UVC method.\n    return X, Y, U, V, C\n\n\ndef _check_consistent_shapes(*arrays):\n    all_shapes = {a.shape for a in arrays}\n    if len(all_shapes) != 1:\n        raise ValueError('The shapes of the passed in arrays do not match')\n\n\nclass Quiver(mcollections.PolyCollection):\n    \"\"\"\n    Specialized PolyCollection for arrows.\n","sourceCodeStart":466,"sourceCodeEnd":502,"githubUrl":"https://github.com/matplotlib/matplotlib/blob/b379c1b69e012b142c0f496a52bcb30513802d72/lib/matplotlib/quiver.py#L466-L502","documentation":"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.","triggerScenarios":"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.","commonSituations":"Station/vector plots built from unequal coordinate arrays; partially flattening grid coordinates; passing row coords and column coords of different lengths without meshgridding first.","solutions":["Make positions equal length: len(x) == len(y) == u.ravel().size == v.ravel().size","Or pass full 2-D grids: X, Y = np.meshgrid(x, y) with u, v of shape Y.shape","Or drop X and Y entirely: plt.quiver(u, v) places arrows at integer index positions","Pre-validate: x = np.asarray(x).ravel(); assert x.size == np.asarray(y).ravel().size"],"exampleFix":"# before\nx = np.linspace(0, 1, 5)\ny = np.linspace(0, 1, 3)\nplt.quiver(x, y, u, v)  # ValueError: X.size is 5 and Y.size is 3\n\n# after\nX, Y = np.meshgrid(x, y)   # shapes match u, v of shape (3, 5)\nplt.quiver(X, Y, u, v)","handlingStrategy":"validation","validationCode":"import numpy as np\nx, y, u = np.asarray(x), np.asarray(y), np.asarray(u)\nnr, nc = (1, u.shape[0]) if u.ndim == 1 else u.shape\nx, y = x.ravel(), y.ravel()\nassert (len(x) == nc and len(y) == nr) or len(x) == len(y), 'X/Y sizes incompatible with U'\nplt.quiver(x, y, u, v)","typeGuard":null,"tryCatchPattern":"try:\n    ax.quiver(x, y, u, v)\nexcept ValueError as e:\n    if 'X and Y must be the same size' in str(e):\n        X, Y = np.meshgrid(np.ravel(x), np.ravel(y))\n        ax.quiver(X, Y, u, v)\n    else:\n        raise","preventionTips":["Generate positions with np.meshgrid whenever U/V are 2-D","For scattered positions flatten everything: x.size == y.size == u.size == v.size","Pre-validate sizes with np.atleast_1d(...).ravel() before plotting"],"tags":["matplotlib","quiver","barbs","vector-field","shape-mismatch","coordinates"],"backgroundTag":"array-shape-mismatch","analyzedSha":"b379c1b69e012b142c0f496a52bcb30513802d72","analyzedAt":"2026-08-21T23:31:55.468Z","schemaVersion":2},"datasetVersion":"2026-08-22T04:17:13.399Z"}