【发布时间】:2012-07-14 06:38:13
【问题描述】:
我有以下数据集:
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']
问题
仅使用.plot() 方法是否有一种优雅的方式来分配多个标签?
【问题讨论】:
标签: matplotlib label legend