{"record":{"id":"71d560851584e891","repo":"matplotlib/matplotlib","slug":"size-mismatch-between-values-and-edges-expect","errorCode":null,"errorMessage":"Size mismatch between \"values\" and \"edges\". Expected `len(values) + 1 == len(edges)`, but `len(values) = {self._values.size}` and `len(edges) = {self._edges.size}`.","messagePattern":"Size mismatch between \"values\" and \"edges\"\\. Expected `len\\(values\\) \\+ 1 == len\\(edges\\)`, but `len\\(values\\) = (.+?)` and `len\\(edges\\) = (.+?)`\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"lib/matplotlib/patches.py","lineNumber":1149,"sourceCode":"            path is drawn.\n\n        **kwargs\n            `Patch` properties:\n\n            %(Patch:kwdoc)s\n        \"\"\"\n        self.orientation = orientation\n        self._edges = np.asarray(edges)\n        self._values = np.asarray(values)\n        self._baseline = np.asarray(baseline) if baseline is not None else None\n        self._update_path()\n        super().__init__(self._path, **kwargs)\n\n    def _update_path(self):\n        if np.isnan(np.sum(self._edges)):\n            raise ValueError('Nan values in \"edges\" are disallowed')\n        if self._edges.size - 1 != self._values.size:\n            raise ValueError('Size mismatch between \"values\" and \"edges\". '\n                             \"Expected `len(values) + 1 == len(edges)`, but \"\n                             f\"`len(values) = {self._values.size}` and \"\n                             f\"`len(edges) = {self._edges.size}`.\")\n        # Initializing with empty arrays allows supporting empty stairs.\n        verts, codes = [np.empty((0, 2))], [np.empty(0, dtype=Path.code_type)]\n\n        _nan_mask = np.isnan(self._values)\n        if self._baseline is not None:\n            _nan_mask |= np.isnan(self._baseline)\n        for idx0, idx1 in cbook.contiguous_regions(~_nan_mask):\n            x = np.repeat(self._edges[idx0:idx1+1], 2)\n            y = np.repeat(self._values[idx0:idx1], 2)\n            if self._baseline is None:\n                y = np.concatenate([y[:1], y, y[-1:]])\n            elif self._baseline.ndim == 0:  # single baseline value\n                y = np.concatenate([[self._baseline], y, [self._baseline]])\n            elif self._baseline.ndim == 1:  # baseline array\n                base = np.repeat(self._baseline[idx0:idx1], 2)[::-1]","sourceCodeStart":1131,"sourceCodeEnd":1167,"githubUrl":"https://github.com/matplotlib/matplotlib/blob/b379c1b69e012b142c0f496a52bcb30513802d72/lib/matplotlib/patches.py#L1131-L1167","documentation":"StepPatch models values as bin heights and edges as bin boundaries (histogram semantics): it requires len(values) + 1 == len(edges), checked as _edges.size - 1 != _values.size. Passing edge counts that don't satisfy this raises the 'Size mismatch between \"values\" and \"edges\"' ValueError, both in the constructor and after set_data.","triggerScenarios":"StepPatch(y, edges=x) where x has the same length as y (point coordinates, not boundaries); using ax.plot(x, y)-style inputs; passing np.arange(len(y)) instead of np.arange(len(y) + 1); mixing up conventions with ax.step(x, y, where='pre') which uses equal-length arrays.","commonSituations":"Porting line/stair plots from ax.step (N edges) to StepPatch (N+1 edges); converting from plt.hist outputs (which already give N+1 bins — correct) versus hand-built arrays; half-open interval confusion when generating time-period boundaries.","solutions":["Provide one more edge than values: edges = np.arange(len(values) + 1) or edges = np.concatenate([[x[0] - 0.5], x + 0.5]).","Reuse histogram machinery: _, edges = np.histogram(values_sample, bins=...) then pass those edges.","If your x really are point positions, use ax.step(x, y) instead of StepPatch.","Add an assertion before constructing: assert len(edges) == len(values) + 1."],"exampleFix":"# before\nsp = StepPatch(values=y, edges=x)  # len(x) == len(y) -> ValueError\n\n# after\nedges = np.concatenate([[x[0] - 0.5], x[:-1] + 0.5, [x[-1] + 0.5]])\nsp = StepPatch(values=y, edges=edges)  # len(edges) == len(y) + 1","handlingStrategy":"validation","validationCode":"import numpy as np\n\nvalues = np.asarray(values)\nedges = np.asarray(edges)\nassert edges.size == values.size + 1, (\n    f'need len(edges) == len(values) + 1, got {edges.size} vs {values.size}')\nsp = patches.StepPatch(values, edges)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Remember StepPatch uses histogram semantics: N values need N+1 edges.","Prefer generating edges from values than passing paired x arrays from line plots.","Add an assert at the boundary so mismatches surface with your own message."],"tags":["matplotlib","patches","steppatch","histogram","length-mismatch"],"backgroundTag":"array-length-mismatch","analyzedSha":"b379c1b69e012b142c0f496a52bcb30513802d72","analyzedAt":"2026-08-21T23:31:55.468Z","schemaVersion":2},"datasetVersion":"2026-08-22T04:17:13.399Z"}