2017-04-15 93 views
2

我得到了我字典Python字典劇情matplotlib

lr = {'0': 0.1354364, '1': 0.134567, '2': 0.100000} 

等等不勝枚舉。

我嘗試ploting與key(0,1,2)簡單的線圖的x軸和y值

plt.plot(lr.keys(),lr.values()) 
plt.title('ID model model accuracy') 
plt.ylabel('accuracy') 
plt.xlabel('epoch') 
plt.legend(['train', 'test'], loc='upper left') 
plt.savefig('ID modelo: model accuracy.png') 
plt.clf() 

(0.1354364,0.134567,0.100000),我得到這個錯誤。

TypeError: float() argument must be a string or a number, not 'dict_keys'

回答

1

這是因爲該方法dictkeys()對象返回其不受pyplot支撐的dict_keys對象。

我會投的dict_keys對象爲list,使這項工作:

plt.plot(list(lr.keys()),list(lr.values())) 

正如mentionned由@nikjohn:

It should be mentioned that this is only required for Python 3 environments. Python 2.7 and the latest matplotlib version, are perfectly okay with the code posted in the question.

+1

它的工作,謝謝! – tanaka

+1

@LucG - 應該指出的是,這僅僅是Python 3環境所必需的。 Python 2.7和最新的'matplotlib'版本,與問題中發佈的代碼完全一致。 – nikjohn