{"record":{"id":"3bf151ee628fc669","repo":"matplotlib/matplotlib","slug":"xdata-must-be-a-sequence","errorCode":null,"errorMessage":"xdata must be a sequence","messagePattern":"xdata must be a sequence","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"lib/matplotlib/lines.py","lineNumber":362,"sourceCode":"                 ):\n        \"\"\"\n        Create a `.Line2D` instance with *x* and *y* data in sequences of\n        *xdata*, *ydata*.\n\n        Additional keyword arguments are `.Line2D` properties:\n\n        %(Line2D:kwdoc)s\n\n        See :meth:`set_linestyle` for a description of the line styles,\n        :meth:`set_marker` for a description of the markers, and\n        :meth:`set_drawstyle` for a description of the draw styles.\n\n        \"\"\"\n        super().__init__()\n\n        # Convert sequences to NumPy arrays.\n        if not np.iterable(xdata):\n            raise RuntimeError('xdata must be a sequence')\n        if not np.iterable(ydata):\n            raise RuntimeError('ydata must be a sequence')\n\n        linewidth = mpl._val_or_rc(linewidth, 'lines.linewidth')\n        linestyle = mpl._val_or_rc(linestyle, 'lines.linestyle')\n        marker = mpl._val_or_rc(marker, 'lines.marker')\n        color = mpl._val_or_rc(color, 'lines.color')\n        markersize = mpl._val_or_rc(markersize, 'lines.markersize')\n        antialiased = mpl._val_or_rc(antialiased, 'lines.antialiased')\n        dash_capstyle = mpl._val_or_rc(dash_capstyle, 'lines.dash_capstyle')\n        dash_joinstyle = mpl._val_or_rc(dash_joinstyle, 'lines.dash_joinstyle')\n        solid_capstyle = mpl._val_or_rc(solid_capstyle, 'lines.solid_capstyle')\n        solid_joinstyle = mpl._val_or_rc(solid_joinstyle, 'lines.solid_joinstyle')\n\n        if drawstyle is None:\n            drawstyle = 'default'\n\n        self._dashcapstyle = None","sourceCodeStart":344,"sourceCodeEnd":380,"githubUrl":"https://github.com/matplotlib/matplotlib/blob/b379c1b69e012b142c0f496a52bcb30513802d72/lib/matplotlib/lines.py#L344-L380","documentation":"Line2D.__init__ requires xdata to be iterable; np.iterable(xdata) returned False, so a scalar number, None, or other non-iterable object was passed as the x data. matplotlib stores line data as arrays and cannot construct a line artist from a single bare value.","triggerScenarios":"Constructing Line2D directly with scalars: Line2D(3, 4); Line2D(np.float64(1.0), y); Line2D(None, y); passing a bare Python or numpy scalar object that defines no __iter__.","commonSituations":"Bypassing ax.plot (which wraps scalars into length-1 arrays) by instantiating Line2D directly; plotting inside a loop and passing the loop variable instead of the collected list; passing an unpacked scalar where a sequence was expected.","solutions":["Wrap the scalar in a list: Line2D([3], [4])","Use ax.plot(3, 4) or ax.scatter([3], [4]), which coerce scalar arguments for you","Collect values into a list first when plotting in a loop, then construct the line once"],"exampleFix":"# before\nline = Line2D(3, 4)\n# after\nline = Line2D([3], [4])","handlingStrategy":"type-guard","validationCode":"import numpy as np\nif not np.iterable(xdata):\n    xdata = [xdata]\nline = Line2D(xdata, ydata)","typeGuard":"import numpy as np\n\ndef is_sequence(v) -> bool:\n    return np.iterable(v) and not isinstance(v, (str, bytes))","tryCatchPattern":"try:\n    line = Line2D(x, y)\nexcept RuntimeError as err:\n    if 'must be a sequence' in str(err):\n        x = [x] if not np.iterable(x) else x\n        line = Line2D(x, y)\n    else:\n        raise","preventionTips":["Never construct Line2D with bare numbers; wrap single points in lists","Prefer ax.plot / ax.scatter, which coerce scalar arguments","Assert np.iterable on data from dynamic sources before passing it to low-level artists"],"tags":["matplotlib","lines","type-validation","sequence"],"backgroundTag":"argument-not-iterable","analyzedSha":"b379c1b69e012b142c0f496a52bcb30513802d72","analyzedAt":"2026-08-21T23:31:55.468Z","schemaVersion":2},"datasetVersion":"2026-08-22T04:17:13.399Z"}