How to plot collections.Counter histogram using matplotlib?
I'm guessing this is what you want to do? You'd just have to add xtick labels(see matplotlib documentation)
import matplotlib.pyplot as plt
import collections
l = ['a', 'b', 'b', 'b', 'c']
count = collections.Counter(l)
print(count)
plt.bar(range(len(count)), count.values())
plt.show()
Looking at your data and attempt, I guess you want a bar plot instead of a histogram. Histogram is used to plot a distribution but that is not what you have. You can simply use the keys
and values
as the arguments of plt.bar
. This way, the keys will be automatically taken as the x-axis tick-labels.
import collections
import matplotlib.pyplot as plt
l = ['a', 'b', 'b', 'b', 'c']
w = collections.Counter(l)
plt.bar(w.keys(), w.values())