shaare.it

TypeError: 'Line2D' object is not iterable

TypeError: ‘Line2D’ object is not iterable

$ python -c "import matplotlib.pyplot as plt; line = plt.plot([1,2,3]); for x,y in line: pass"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: 'Line2D' object is not iterable

Why this happens

You attempted to iterate over a Line2D object returned by plotting functions.

Fix

Unpack the list returned by plot or handle the Line2D object directly.

Wrong code

import matplotlib.pyplot as plt
line = plt.plot([1,2,3])
for x, y in line:
    pass

Fixed code

import matplotlib.pyplot as plt
line, = plt.plot([1,2,3])
line.set_color('red')