Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Counter() in Python to build histogram?

I saw on another question that I could use Counter() to count the number of occurrences in a set of strings. So if I have ['A','B','A','C','A','A'] I get Counter({'A':3,'B':1,'C':1}). But now, how can I use that information to build a histogram for example?

like image 390
marc Avatar asked Oct 05 '13 14:10

marc


People also ask

What does counter () do in Python?

Counter is a subclass of dict that's specially designed for counting hashable objects in Python. It's a dictionary that stores objects as keys and counts as values. To count with Counter , you typically provide a sequence or iterable of hashable objects as an argument to the class's constructor.

How do you make a histogram in Python?

To create a histogram the first step is to create bin of the ranges, then distribute the whole range of the values into a series of intervals, and count the values which fall into each of the intervals. Bins are clearly identified as consecutive, non-overlapping intervals of variables. The matplotlib.


2 Answers

For your data it is probably better to use a barchart instead of a histogram. Check out this code:

from collections import Counter import numpy as np import matplotlib.pyplot as plt   labels, values = zip(*Counter(['A','B','A','C','A','A']).items())  indexes = np.arange(len(labels)) width = 1  plt.bar(indexes, values, width) plt.xticks(indexes + width * 0.5, labels) plt.show() 

Result: enter image description here

like image 83
Igonato Avatar answered Oct 14 '22 03:10

Igonato


You can write some really concise code to do this using pandas:

    import numpy as np      from pandas import Series          sample = np.random.choice(['a', 'b'], size=10)          s = Series(sample)          In [29]: s     Out[29]:     0    a     1    b     2    b     3    b     4    a     5    b     6    b     7    b     8    b     9    a     dtype: object          vc = s.value_counts()          In [31]: vc     Out[31]:     b    7     a    3     dtype: int64          vc = vc.sort_index()          In [33]: vc     Out[33]:     a    3     b    7     dtype: int64          c.plot(kind='bar') 

Resulting in:

enter image description here

like image 32
Phillip Cloud Avatar answered Oct 14 '22 01:10

Phillip Cloud