7

I got I dictionary

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

and so goes on.

I try ploting a simple line graph with key(0,1,2) as the x axis and the value (0.1354364,0.134567,0.100000) as the y value

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

And I got this error.

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

0

1 Answer 1

9

This is because the method keys() of dict objects returns a dict_keys object which is not supported by pyplot.

I would cast the dict_keys object into list to make this work:

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

As mentionned by @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.

Sign up to request clarification or add additional context in comments.

1 Comment

@LucG - 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.