2016-12-05 24 views
-1

我試圖展示我的傳奇人物內外,但仍然看不到它。它只是空box.what的錯誤我的傳奇沒有出現

p1=plt.plot(np.logspace(-2,1,10), trainsScores, label="train scores") 
p2=plt.plot(np.logspace(-2,1,10), testScores, label="test scores") 
plt.legend([p1, p2], ["Train score", "Test score"], loc='upper center',bbox_to_anchor=(0.5, -0.05), 
fancybox=True, shadow=True, ncol=5) 
plt.xlabel('C') 
plt.ylabel('Score') 
plt.show() 

enter image description here

回答

2

你沒收到警告印在控制檯上?

UserWarning: Legend does not support [<matplotlib.lines.Line2D object at 0x7f7a9a442518>] instances. 

在那裏你有解釋。 p1p2是列表,您不能將列表作爲圖例句柄傳遞。

>>> print(type(p1)) 
<class 'list'> 

分配Line2D實例p1p2,它會工作。

p1, = plt.plot(np.logspace(-2,1,10), np.random.rand(10), label="train scores") 
p2, = plt.plot(np.logspace(-2,1,10), np.random.rand(10), label="test scores") 
plt.legend([p1, p2], ["Train score", "Test score"], loc='upper center', 
      bbox_to_anchor=(0.5, -0.05), fancybox=True, shadow=True, ncol=5) 
plt.xlabel('C') 
plt.ylabel('Score') 
plt.show() 

enter image description here