Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 590
Kosh Avatar asked Feb 06 '26 10:02

Kosh


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

like image 175
Sheldore Avatar answered Feb 09 '26 03:02

Sheldore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!