matplotlib/matplotlib · error · RuntimeError

x must be a sequence

Error message

x must be a sequence

What it means

Line2D.set_xdata stores the x data and requires it to be iterable; np.iterable(x) returned False, so a scalar was passed. Unlike the higher-level plotting APIs there is no coercion here: the value is copied into _xorig as-is and must already be a sequence.

Source

Thrown at lib/matplotlib/lines.py:1339

        if self._markersize != sz:
            self.stale = True
        self._markersize = sz

    def set_xdata(self, x):
        """
        Set the data array for x.

        Parameters
        ----------
        x : 1D array

        See Also
        --------
        set_data
        set_ydata
        """
        if not np.iterable(x):
            raise RuntimeError('x must be a sequence')
        self._xorig = copy.copy(x)
        self._invalidx = True
        self.stale = True

    def set_ydata(self, y):
        """
        Set the data array for y.

        Parameters
        ----------
        y : 1D array

        See Also
        --------
        set_data
        set_xdata
        """
        if not np.iterable(y):

View on GitHub (pinned to b379c1b69e)

Solutions

  1. Wrap scalars in a list: line.set_xdata([5])
  2. To clear a line use empty sequences: line.set_data([], [])
  3. Keep a buffer array and assign the buffer or slices of it, never scalar reductions of it

Example fix

# before
line.set_xdata(new_x_value)
# after
line.set_xdata([new_x_value])
Defensive patterns

Strategy: type-guard

Validate before calling

import numpy as np
if not np.iterable(x):
    x = [x]
line.set_xdata(x)

Type guard

import numpy as np

def is_sequence(v) -> bool:
    return np.iterable(v) and not isinstance(v, (str, bytes))

Prevention

When it happens

Trigger: line.set_xdata(5); line.set_xdata(np.float64(2.5)); line.set_xdata(None) when trying to clear the line.

Common situations: Live-update/animation code assigning a freshly computed scalar (for example a rolling last value) instead of a length-1 array; replacing arrays with None to 'clear' a line instead of using set_data([], []).

Related errors


AI-assisted analysis of matplotlib/matplotlib@b379c1b69e (2026-08-21). Data as JSON: /api/errors/4ab0ddce23e0b5f5. Report an issue: GitHub.