【问题标题】:How to properly create hist in matplotlib?如何在 matplotlib 中正确创建 hist?
【发布时间】:2016-08-05 14:06:15
【问题描述】:

我想绘制一个三列高度为51020 的直方图。每列的宽度为 1。因此,第一列在间隔 [0,1] 上的高度为 5,第二列在间隔 [1,2] 上的高度为 10,依此类推。

plt.hist([5, 10, 20], bins=range(0,4,1))
plt.show() 

但结果我什么都没有:

我做错了什么?

【问题讨论】:

    标签: python matplotlib histogram


    【解决方案1】:

    hist 计算位于给定 bin 内的数据样本数,然后将结果频率显示为条形图。您实际上并不需要hist,因为您已经拥有频率。您只需要bar 将这些频率显示为条形图。第一个输入指定每个条的左边缘位置,然后我们可以使用width kwarg 指定每个条的宽度。

    import matplotlib.pyplot as plt
    
    plt.bar([0, 1, 2], [5, 10, 20], width=1)
    

    【讨论】:

    • 您能再回答一次这个问题吗?我已经更新了。
    • @RomaKarageorgievich 已更新。
    猜你喜欢
    • 2022-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多