Print dictionary using matplotlib

Asked

Viewed 300 times

0

Hello,

I have a dictionary of the type: and I would like to print a line chart with the key k as x and v as y.

tried a lot of things but keep getting errors:

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

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

  • What is the dictionary structure ?

1 answer

0


In a dictionary, when you ask only the .keys() or .values() he gives the answer along with a dict_keys or dict_values.

That’s what’s happening there in your code, the type number float is coming along with this dict who’s making this mistake.

That’s what I think, noting what you put in the question. If you can put more things so that we can see better.

Behold, example:

dic = {'teste':'teste2'}

>>> print(dic.keys())
>>> dict_keys(['teste']) #Resultado, com dict
>>>
>>> print(dic.values())
>>> dict_values(['teste2']) #Resultado, com dict

To solve:

>>> print(list(dic.keys()))
>>> ['teste'] #Resultado, sem dict
>>>
>>> print(list(dic.values()))
>>> ['teste2'] #Resultado, sem dict

try to adapt to your code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.