How to plot histogram of following Counter object?:
w = collections.Counter()
l = ['a', 'b', 'b', 'b', 'c']
for o in l:
w[o]+=1
To display the count over the bar in matplotlib histogram, we can iterate each patch and use text() method to place the values over the patches.
Set the figure size and adjust the padding between and around the subplots. Make two dataframes, df1 and df2, of two-dimensional, size-mutable, potentially heterogeneous tabular data. Create a figure and a set of subplots.
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())
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()
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