Is it possible to switch X axis in Python matplotlib.pyplot.hist from bin edges to exact values?

Viewed 111

Is it possible to switch X axis in Python matplotlib.pyplot.hist from bin edges to exact values? In other words this is what I get:

dataset = [0,1,1,1,2,2,3,3,4]
plt.hist(dataset, 5, rwidth=0.9)

enter image description here

and this what I need: enter image description here

1 Answers

You can first compute the frequencies and then use a bar plot

from collections import Counter
import matplotlib.pyplot as plt

dataset = [0,1,1,1,2,2,3,3,4]

freqs = Counter(dataset)

plt.bar(freqs.keys(), freqs.values(), width=0.9)
plt.show()

enter image description here

Related