Pablo
Pablo

Reputation: 11031

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:

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.

Upvotes: 3

Views: 4837

Answers (2)

Skippy le Grand Gourou
Skippy le Grand Gourou

Reputation: 7694

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)

Upvotes: 0

joemar.ct
joemar.ct

Reputation: 1216

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.

Upvotes: 5

Related Questions