{"record":{"id":"00c507feffd0378e","repo":"matplotlib/matplotlib","slug":"for-x-width-and-y-height-with-self-shadi","errorCode":null,"errorMessage":"For X ({width}) and Y ({height}) with {self._shading} shading, A should have shape {' or '.join(map(str, ok_shapes))}, not {A.shape}","messagePattern":"For X \\((.+?)\\) and Y \\((.+?)\\) with (.+?) shading, A should have shape (.+?), not (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"lib/matplotlib/collections.py","lineNumber":2387,"sourceCode":"\n            If the values are provided as a 2D grid, the shape must match the\n            coordinates grid. If the values are 1D, they are reshaped to 2D.\n            M, N follow from the coordinates grid, where the coordinates grid\n            shape is (M, N) for 'gouraud' *shading* and (M+1, N+1) for 'flat'\n            shading.\n        \"\"\"\n        height, width = self._coordinates.shape[0:-1]\n        if self._shading == 'flat':\n            h, w = height - 1, width - 1\n        else:\n            h, w = height, width\n        ok_shapes = [(h, w, 3), (h, w, 4), (h, w), (h * w,)]\n        if A is not None:\n            if hasattr(self, 'norm'):\n                A = mcolorizer._ensure_multivariate_data(A, self.norm.n_components)\n            shape = np.shape(A)\n            if shape not in ok_shapes:\n                raise ValueError(\n                    f\"For X ({width}) and Y ({height}) with {self._shading} \"\n                    f\"shading, A should have shape \"\n                    f\"{' or '.join(map(str, ok_shapes))}, not {A.shape}\")\n        return super().set_array(A)\n\n    def get_coordinates(self):\n        \"\"\"\n        Return the vertices of the mesh as an (M+1, N+1, 2) array.\n\n        M, N are the number of quadrilaterals in the rows / columns of the\n        mesh, corresponding to (M+1, N+1) vertices.\n        The last dimension specifies the components (x, y).\n        \"\"\"\n        return self._coordinates\n\n    def get_edgecolor(self):\n        # docstring inherited\n        # Note that we want to return an array of shape (N*M, 4)","sourceCodeStart":2369,"sourceCodeEnd":2405,"githubUrl":"https://github.com/matplotlib/matplotlib/blob/b379c1b69e012b142c0f496a52bcb30513802d72/lib/matplotlib/collections.py#L2369-L2405","documentation":"QuadMesh.set_array (backing pcolormesh) validates that the color array A matches the mesh geometry. With shading='flat' and (height, width) vertex coordinates, A must be (height-1, width-1), optionally with a trailing 3 or 4 for RGB(A), or flat size (height-1)*(width-1); with 'gouraud'/'nearest' shading A must match the full (height, width). The message lists the exact shapes expected for your mesh.","triggerScenarios":"ax.pcolormesh(X, Y, C) where C's shape does not match X/Y under the active shading; mesh.set_array(arr) with a wrong-shape array; the classic off-by-one of passing (M+1, N+1) corner coordinates with a (M+1, N+1) color array under flat shading; passing an RGBA array whose first two dims do not match the mesh.","commonSituations":"Mixing imshow-style arrays (same shape as coordinates) with pcolormesh's cell-based convention; upgrading matplotlib versions where the default shading changed (older code relying on implicit 'flat' with same-shape inputs now needs shading='nearest'); swapping in a downsampled color array while keeping full-resolution coordinates.","solutions":["Match A to the mesh: for flat shading with (M+1, N+1) coordinates pass A of shape (M, N); for 'nearest'/'gouraud' pass A of the same shape as the coordinates","Reshape or slice the color array to one of the accepted shapes listed in the message (2-D, 3-D with 3/4 channels, or flattened)","Use shading='auto' to let matplotlib infer the convention from the input shapes","If mutating later, call set_array with an array of the same shape as the original"],"exampleFix":"// before\nX, Y = np.meshgrid(x, y)          # (11, 11)\nmesh = ax.pcolormesh(X, Y, C11)   # C11 is (11, 11), shading='flat' -> error\n// after\nmesh = ax.pcolormesh(X, Y, C10)   # flat shading: A is (10, 10) cells\n// or\nmesh = ax.pcolormesh(X, Y, C11, shading='nearest')","handlingStrategy":"validation","validationCode":"import numpy as np\n\ndef expected_a_shapes(coords, shading):\n    h, w = np.asarray(coords).shape[:2]\n    if shading == 'flat':\n        h, w = h - 1, w - 1\n    return {(h, w, 3), (h, w, 4), (h, w), (h * w,)}\n\n# before set_array / pcolormesh\nif np.shape(A) not in expected_a_shapes(coords, shading):\n    raise ValueError(f'A must be one of {expected_a_shapes(coords, shading)}, got {np.shape(A)}')","typeGuard":"def quadmesh_array_ok(coords, A, shading):\n    return np.shape(A) in expected_a_shapes(coords, shading)","tryCatchPattern":"try:\n    mesh.set_array(A)\nexcept ValueError as e:\n    if 'should have shape' in str(e):\n        A = A.reshape(expected_shape)  # or log and skip the update\n        mesh.set_array(A)\n    else:\n        raise","preventionTips":["Standardize on shading='auto' so matplotlib reconciles coordinate and color shapes","Encapsulate the coords/C shape pairing in one helper so the off-by-one cannot diverge across the codebase","Unit-test plotting helpers with both (M, N) and (M+1, N+1) coordinate grids"],"tags":["matplotlib","pcolormesh","quadmesh","numpy","shape","validation"],"backgroundTag":"array-shape-mismatch","analyzedSha":"b379c1b69e012b142c0f496a52bcb30513802d72","analyzedAt":"2026-08-21T23:31:55.468Z","schemaVersion":2},"datasetVersion":"2026-08-22T04:17:13.399Z"}