簡體   English   中英

如何在python中使用matplotlib為重疊圖的圖例添加兩個不同的標簽?

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

好吧,即使我在網上搜索但無法獲得它,這可能很容易。 基本上我想將兩個不同的標簽添加到我的情節中,這是我的代碼行

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")

但我得到以下結果,其中兩個 plot.as 具有相同的標簽

在此處輸入圖片說明

即使我試過這個

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 

但給出錯誤:

SyntaxError: keyword argument repeated

這個簡單方法的鏈接指南,但這里plt.plot()在接受的答案中使用了兩次。

你需要兩行。 一為劇情,一為傳奇。

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