Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting an histogram from a dictionary values in Python

I am trying to plot an histogram. The data for this histogram come from a dictionary that contains a frequency list, and all I need is to plot either:

  • A histogram or,

  • A barplot of the values of each element (the histogram can be derived from here : ) )

Here's an example of what the dictionary looks like:

{0: 282, 1: 152, 2: 131, 3: 122, 4: 108, 5: 101, 6: 106, 7: 91, 8: 96, 9: 92,
...
1147: 1, 1157: 1, 1186: 1, 1217: 1, 1236: 1, 1251: 1, 1255: 1, 1291: 1, 1372: 1, 1402: 1}

Thanks a lot.

like image 840
Pablo Avatar asked Jun 18 '26 07:06

Pablo


1 Answers

You can do this quickly using pandas:

import pandas as pd

df = pd.DataFrame([your_dict])
df = df.T
df.hist()

Note, your_dict should be inside a list.

like image 187
joemar.ct Avatar answered Jun 20 '26 21:06

joemar.ct