简体   繁体   English

如何在python中使用matplotlib为重叠图的图例添加两个不同的标签?

[英]How to add two different labels to legend for an overlap plot using matplotlib in python?

Okay this might be easy even I search on web but could not get it.好吧,即使我在网上搜索但无法获得它,这可能很容易。 Basically i want to add the two different labels to my plot and this is my line of code for that基本上我想将两个不同的标签添加到我的情节中,这是我的代码行

plt.plot(x[:,1],y,'ro',x[:,1],Line_fit,'b',linewidth=0.5,markersize=4,label="training data") # plot the data 
plt.legend(loc="upper left")

but I am getting following result in which has same labels for both the plot.as following但我得到以下结果,其中两个 plot.as 具有相同的标签

在此处输入图片说明

Even I tried this即使我试过这个

plt.plot(x[:,1],y,'ro',x[:,1],Line_fit,'b',linewidth=0.5,markersize=4,label="training data",label="Linear Regression") # plot the data 

but give the error:但给出错误:

SyntaxError: keyword argument repeated

This link guide for the simple way but here plt.plot() had used twice in the accepted answer.My question is how can i do it the same thing in single line code as I did in my code ? 这个简单方法的链接指南,但这里plt.plot()在接受的答案中使用了两次。

You need two lines.你需要两行。 One for the plot, one for the legend.一为剧情,一为传奇。

plt.plot(x[:,1], y, 'ro', x[:,1], Line_fit, 'b', linewidth=0.5, markersize=4)
plt.legend(["training data", "Linear Regression"], loc="upper left")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM