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

Kos*_*osh 3 matplotlib histogram python-3.x

是否可以将 Python matplotlib.pyplot.hist 中的 X 轴从 bin 边缘切换到精确值?换句话说,这就是我得到的:

dataset = [0,1,1,1,2,2,3,3,4]
plt.hist(dataset, 5, rwidth=0.9)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

这就是我需要的: 在此处输入图片说明

She*_*ore 5

您可以先计算频率,然后使用条形图

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()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明