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'
values() is an inbuilt method in Python programming language that returns a view object. The view object contains the values of the dictionary, as a list. If you use the type() method on the return value, you get “dict_values object”.
The plot() function is used to draw points (markers) in a diagram. By default, the plot() function draws a line from point to point. The function takes parameters for specifying points in the diagram.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With