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?
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.
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.
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:
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With