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

标签 python-3.x matplotlib histogram

是否可以将 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

最佳答案

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

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

关于python-3.x - 是否可以将 Python matplotlib.pyplot.hist 中的 X 轴从 bin 边缘切换为精确值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62310216/

相关文章:

Python Matplotlib 绘图 : Set aspect ratio of output file (e. g。图片)

matplotlib - Matplotlib 子图Fig 和axs 变量的用途是什么

r - 在两个 ggplot 直方图上显示均值和中值

python - 绘制直方图后更改其颜色

python - 如何创建 tkinter 错误消息框

python 3.2 : pygame. KEYDOWN 只能工作 1 次?

python - 以与轴的纵横比一致的物理单位重新缩放箭矢箭头

python - 如何在 Python 中读取 txt 文件的特定部分?

python - 在 python 列表理解中有没有一种方法可以不重复工作

python - 如何在 matplotlib 中定位单个箱线图?