Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

Another way without using pandas :

h = [v for v in your_dict.values()]
plt.hist(h)

This allows easy filtering and modifying, e.g.

h = [np.log(v) for v in your_dict.values() if v>0]
plt.hist(v)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top