{"record":{"id":"705797ac73cc6073","repo":"matplotlib/matplotlib","slug":"the-first-element-of-code-must-be-equal-to-move","errorCode":null,"errorMessage":"The first element of 'code' must be equal to 'MOVETO' ({self.MOVETO}).  Your first code is {codes[0]}","messagePattern":"The first element of 'code' must be equal to 'MOVETO' \\((.+?)\\)\\.  Your first code is (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"lib/matplotlib/path.py","lineNumber":139,"sourceCode":"            line segments of a closed polygon.  Note that the last vertex will\n            then be ignored (as the corresponding code will be set to\n            `CLOSEPOLY`).\n        readonly : bool, optional\n            Makes the path behave in an immutable way and sets the vertices\n            and codes as read-only arrays.\n        \"\"\"\n        vertices = _to_unmasked_float_array(vertices)\n        _api.check_shape((None, 2), vertices=vertices)\n\n        if codes is not None and len(vertices):\n            codes = np.asarray(codes, self.code_type)\n            if codes.ndim != 1 or len(codes) != len(vertices):\n                raise ValueError(\"'codes' must be a 1D list or array with the \"\n                                 \"same length of 'vertices'. \"\n                                 f\"Your vertices have shape {vertices.shape} \"\n                                 f\"but your codes have shape {codes.shape}\")\n            if len(codes) and codes[0] != self.MOVETO:\n                raise ValueError(\"The first element of 'code' must be equal \"\n                                 f\"to 'MOVETO' ({self.MOVETO}).  \"\n                                 f\"Your first code is {codes[0]}\")\n        elif closed and len(vertices):\n            codes = np.empty(len(vertices), dtype=self.code_type)\n            codes[0] = self.MOVETO\n            codes[1:-1] = self.LINETO\n            codes[-1] = self.CLOSEPOLY\n\n        self._vertices = vertices\n        self._codes = codes\n        self._interpolation_steps = _interpolation_steps\n        self._update_values()\n\n        if readonly:\n            self._vertices.flags.writeable = False\n            if self._codes is not None:\n                self._codes.flags.writeable = False\n            self._readonly = True","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/matplotlib/matplotlib/blob/b379c1b69e012b142c0f496a52bcb30513802d72/lib/matplotlib/path.py#L121-L157","documentation":"After the shape checks, Path.__init__ additionally requires that a non-empty codes array starts with Path.MOVETO (value 1): every Bezier path must begin with a pen-up move before any line or curve segments. A first code of LINETO, CURVE3, CURVE4, or CLOSEPOLY is undefined behavior in the Agg/renderer path machinery, so it is rejected at construction with a message showing your first code.","triggerScenarios":"Path(verts, [2, 2, 2]) (codes built from LINETO only); slicing codes[1:] and reusing them; concatenating subpath codes where the leading MOVETO was dropped; mapping all codes through a lookup that loses the initial MOVETO.","commonSituations":"Hand-rolled codes arrays that only track LINETO; path assembly from segment pieces where each piece is expected to be self-contained; converting SVG-ish segment lists and forgetting the initial move command.","solutions":["Prepend Path.MOVETO: codes = np.r_[Path.MOVETO, codes]","Or build codes wholesale: [Path.MOVETO] + [Path.LINETO] * (n - 2) + [Path.CLOSEPOLY] for a closed polygon","Or omit codes entirely and use Path(verts, closed=True)","When concatenating subpaths, keep each subpath's leading MOVETO and use Path.make_compound_path / compound path utilities"],"exampleFix":"// before\np = Path(verts, [Path.LINETO] * len(verts))\n// after\np = Path(verts, [Path.MOVETO] + [Path.LINETO] * (len(verts) - 1))","handlingStrategy":"validation","validationCode":"import numpy as np\nfrom matplotlib.path import Path\n\ndef ensure_moveto_first(codes):\n    codes = np.asarray(codes).ravel()\n    if len(codes) and codes[0] != Path.MOVETO:\n        codes = np.r_[Path.MOVETO, codes]\n    return codes","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Every subpath must open with Path.MOVETO (1); CLOSEPOLY always belongs at the end","When concatenating subpaths, keep each leading MOVETO or use Path.make_compound_path","Construct codes from Path class constants (Path.MOVETO, Path.LINETO, ...) not raw integers"],"tags":["matplotlib","path","codes","moveto","validation"],"backgroundTag":"invalid-path-codes","analyzedSha":"b379c1b69e012b142c0f496a52bcb30513802d72","analyzedAt":"2026-08-21T23:31:55.468Z","schemaVersion":2},"datasetVersion":"2026-08-22T04:17:13.399Z"}