{"record":{"id":"bfe744887d4dcbff","repo":"matplotlib/matplotlib","slug":"input-z-must-be-at-least-a-2-2-shaped-array-bu","errorCode":null,"errorMessage":"Input z must be at least a (2, 2) shaped array, but has shape {z.shape}","messagePattern":"Input z must be at least a \\(2, 2\\) shaped array, but has shape (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"lib/matplotlib/contour.py","lineNumber":1416,"sourceCode":"            self.zmin = z.min().astype(float)\n        self._process_contour_level_args(args, z.dtype)\n        return (x, y, z)\n\n    def _check_xyz(self, x, y, z, kwargs):\n        \"\"\"\n        Check that the shapes of the input arrays match; if x and y are 1D,\n        convert them to 2D using meshgrid.\n        \"\"\"\n        x, y = self.axes._process_unit_info([(\"x\", x), (\"y\", y)], kwargs)\n\n        x = np.asarray(x, dtype=np.float64)\n        y = np.asarray(y, dtype=np.float64)\n        z = ma.asarray(z)\n\n        if z.ndim != 2:\n            raise TypeError(f\"Input z must be 2D, not {z.ndim}D\")\n        if z.shape[0] < 2 or z.shape[1] < 2:\n            raise TypeError(f\"Input z must be at least a (2, 2) shaped array, \"\n                            f\"but has shape {z.shape}\")\n        Ny, Nx = z.shape\n\n        if x.ndim != y.ndim:\n            raise TypeError(f\"Number of dimensions of x ({x.ndim}) and y \"\n                            f\"({y.ndim}) do not match\")\n        if x.ndim == 1:\n            nx, = x.shape\n            ny, = y.shape\n            if nx != Nx:\n                raise TypeError(f\"Length of x ({nx}) must match number of \"\n                                f\"columns in z ({Nx})\")\n            if ny != Ny:\n                raise TypeError(f\"Length of y ({ny}) must match number of \"\n                                f\"rows in z ({Ny})\")\n            x, y = np.meshgrid(x, y)\n        elif x.ndim == 2:\n            if x.shape != z.shape:","sourceCodeStart":1398,"sourceCodeEnd":1434,"githubUrl":"https://github.com/matplotlib/matplotlib/blob/b379c1b69e012b142c0f496a52bcb30513802d72/lib/matplotlib/contour.py#L1398-L1434","documentation":"Beyond being 2D, the height field z must be at least 2x2: contouring computes gradients between adjacent cells, so a single row, single column, or single cell leaves nothing to contour. _check_xyz raises TypeError when z.shape[0] < 2 or z.shape[1] < 2. This fires for shapes like (1, 5), (5, 1), and (1, 1).","triggerScenarios":"ax.contour(np.array([[0, 1, 2]])) (one row); passing a coarse 2-point grid shrunk to one axis by a slicing bug (z[:, 0] instead of z[:, :2]); plotting a 1x1 or degenerate raster extracted from a larger array.","commonSituations":"Edge-case datasets that reduce to a single row/column after filtering NaNs or subsetting; unit tests using trivially small arrays; GUI zoom/region-selection features that crop z too aggressively.","solutions":["Supply a z with shape >= (2, 2); check z.shape before calling contour.","Fix slicing bugs: verify the slice keeps both dimensions (z[0:2, 0:2] not z[0, 0:2]).","If the dataset genuinely has one row/column, use ax.plot or ax.scatter instead — contouring is undefined there.","Pad the grid with an extra edge row/column when a boundary slice is unavoidable."],"exampleFix":"# before\nz = np.array([[0.0, 1.0, 2.0]])   # shape (1, 3)\nax.contour(z)                      # TypeError\n\n# after\nz = np.array([[0.0, 1.0, 2.0],\n              [0.5, 1.5, 2.5]])   # shape (2, 3)\nax.contour(z)","handlingStrategy":"validation","validationCode":"z = np.asarray(z)\nif z.ndim != 2 or min(z.shape) < 2:\n    raise ValueError(f'z too small to contour: {z.shape}')","typeGuard":"def contourable(a):\n    a = np.asarray(a)\n    return a.ndim == 2 and a.shape[0] >= 2 and a.shape[1] >= 2","tryCatchPattern":"try:\n    ax.contour(z)\nexcept TypeError:\n    ax.plot(z.ravel())  # degenerate grid: fall back to a line plot","preventionTips":["Check min(z.shape) >= 2 before plotting.","Review slicing code for accidental removal of a whole axis.","Test plotting helpers on tiny edge-case arrays."],"tags":["matplotlib","contour","contourf","shape","typeerror"],"backgroundTag":"array-too-small","analyzedSha":"b379c1b69e012b142c0f496a52bcb30513802d72","analyzedAt":"2026-08-21T23:31:55.468Z","schemaVersion":2},"datasetVersion":"2026-08-22T04:17:13.399Z"}