2017-04-15 127 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