我有以下数据集:
x = [0, 1, 2, 3, 4]
y = [ [0, 1, 2, 3, 4],
[5, 6, 7, 8, 9],
[9, 8, 7, 6, 5] ]
现在我用以下方法绘制它:
import matplotlib.pyplot as plt
plt.plot(x, y)
但是,我想用此命令标记 3 个 y 数据集,这会在以下情况下引发错误.legend()
叫做:
lineObjects = plt.plot(x, y, label=['foo', 'bar', 'baz'])
plt.legend()
File "./plot_nmos.py", line 33, in <module>
plt.legend()
...
AttributeError: 'list' object has no attribute 'startswith'
当我检查lineObjects
:
>>> lineObjects[0].get_label()
['foo', 'bar', 'baz']
>>> lineObjects[1].get_label()
['foo', 'bar', 'baz']
>>> lineObjects[2].get_label()
['foo', 'bar', 'baz']
Question
有没有一种优雅的方法来分配多个标签,只需使用.plot()
method?