我试图绘制一个包含多行和一个图例的虚拟图表,其中列表中的每个值都标有"标签"将为每个相应的行显示一次。
在下面的MWE中,图例的每一行都包含列表的所有值"标签"我想迭代列表。
y = np.random.rand(1,300).reshape(50,6)
x = np.repeat(np.array([range(1,7)]), 50, axis=0)
label = [70, 50, 30, 10, 30, 50]
plt.figure()
plt.plot(x, y, marker='+', label=label)
plt.legend()
plt.show()
如果我尝试在列表上循环,我会获得图例的单个值,但每个值重复多次,我不想重复值:
y = np.random.rand(1,300).reshape(50,6)
x = np.repeat(np.array([range(1,7)]), 50, axis=0)
label = [70, 50, 30, 10, 30, 50]
plt.figure()
for i in label:
plt.plot(x, y, marker='+', label='{i}'.format(i=i))
plt.legend()
plt.show()
任何帮助将不胜感激。 提前谢谢。
答案 0 :(得分:1)
试试这个:
p = plt.plot(x, y, marker='+')
plt.legend(p, label)