有没有可能在Python matplotlib.pyplot.hist中把X轴从bin边缘切换到精确值?

问题描述 投票:0回答:1

是否可以在Python matplotlib.pyplot.hist中把X轴从bin边缘切换到精确值?

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

enter image description here

而这是我需要的。enter image description here

python-3.x matplotlib histogram
1个回答
4
投票

你可以先计算频率,然后用条形图来计算

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

© www.soinside.com 2019 - 2024. All rights reserved.