2017-06-02 96 views
1

我正在學習matplotlib庫,因此試圖在1圖上顯示2行來表示機器學習模型。這個問題與傳說有關。圖例中有一些代碼行。我不知道如何刪除它。有人可以幫助刪除它嗎?Matplotlib圖例顯示了一些意外的代碼行

alldata=plt.plot(x,y,'o') 
l1 =plt.plot(x_new, model1(x_new),'r',linestyle='dashed',linewidth=4) 
l2 =plt.plot(x_new, model2(x_new),'g',linewidth=4) 
plt.title('no. of bench employees needed for new projects over last 5 years') 
plt.xlabel('year') 
plt.ylabel('employees/month') 
plt.xticks([W*12 for W in range(6)],['year %i'%w for w in range(6)]) 
plt.legend([("alldata"),(l1,"d=%i" %model1.order),(l2,"d=%i" %model2.order)], loc=1) 
plt.autoscale(tight=True) 
plt.grid() 
plt.show() 

以上代碼生成該曲線圖中:

graph by above code

+1

你可能想澄清你的問題,你寫「我不知道如何刪除它」,指的是傳說後,允許潛在的conf在你的意思中使用「it」。 – HunterM267

回答

1

如果你看看matplotlib.pyplot.legend文檔,你會注意到你用錯誤的格式調用它。引用文檔:

完全控制它的藝術家有一個圖例項,就可以通過,隨後分別圖例標籤的可迭代的傳奇藝術家的迭代:

legend((line1, line2, line3), ('label1', 'label2', 'label3')) 

所以你需要在兩個元通:

plt.legend((alldata[0], l1[0], l2[0]),        # plots 
      ("alldata","d=%i" % model1.order, "d=%i" % model2.order), # names 
      loc=1) 
0

您可以使用(l1,"d=%i" %model1.order)自己將這些標籤放在那裏。從標籤上取下l1,使其消失。

plt.legend([("d=%i" %model1.order),("d=%i" %model2.order),("alldata")], loc=1) 

或只是

plt.legend(["d=%i" %model1.order,"d=%i" %model2.order, "alldata"], loc=1) 

爲了保持秩序,提供手柄爲好。

plt.legend(handles=[alldata[0], l1[0], l2[0]], 
      labels=["alldata", "d=%i" %model1.order,"d=%i" %model2.order], loc=1) 

一種不同的方法將指定地塊標籤上的繪圖命令裏,

plt.plot(..., label="alldata") 
plt.plot(..., label="label1") 
plt.legend(loc=1) 

這會自動選擇圖例的標籤。