【发布时间】:2017-06-22 20:49:30
【问题描述】:
我正在尝试绘制一个带有几条线和一个图例的虚拟图,其中列表“标签”中的每个值都将为每条对应的线显示一次。
在下面的 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()
任何帮助将不胜感激。 提前致谢。
【问题讨论】:
-
您能否明确说明图例的外观以及显示的标记(及其颜色)应如何与 36 行相关联?你真的想在情节中有 36 行(因为它们看起来是一样的)?我建议你用你想要的线条显示图表,然后描述你想在图例中显示的内容。
标签: python-3.x numpy matplotlib legend