Alon
Alon

Reputation: 695

How to build a histogram from values of Counter?

I used Counter() to count the number of occurrences of keys.

So I have ({'A':1, 'B':3, 'C':1, 'D':2, 'E':1, 'F':3, G:'2'})

How can I use that information to build a histogram for the numbers of values:

Upvotes: 3

Views: 3044

Answers (2)

dejanmarich
dejanmarich

Reputation: 1285

This looks like a dictionary, so I guess you can use bar from matplotlib:

frequency_calculated = ({'A':1, 'B':3, 'C':1, 'D':2, 'E':1, 'F':3, 'G':2})
plt.bar(frequency_calculated.keys(), frequency_calculated.values())

with result:

enter image description here

EDIT

From calculated frequency your data looks like a, and you can plot histogram of that:

a = ('A','B','B','B','C','D','D','E','F','F','F','G','G')
plt.hist(a)

with result:

enter image description here

To display histogram you don't need to calculate frequency. Histogram differs from a bar graph, in the sense that a bar graph relates two variables, but a histogram relates only one.

Difference between bar and histogram

Upvotes: 2

anotherone
anotherone

Reputation: 795

do you want this? corrected your dictionary's last element, as I think you meant what I wrote.

import pandas as pd
data = pd.Series({'A':1, 'B':3, 'C':1, 'D':2, 'E':1, 'F':3, 'G':2})
data.value_counts()

1    3
3    2
2    2
dtype: int64

Upvotes: 1

Related Questions