【问题标题】:Python matplotlib.pyplot: How to make a histogram with bins counts including right bin edge?Python matplotlib.pyplot:如何制作包含右 bin 边缘的 bin 计数的直方图?
【发布时间】:2016-10-19 11:26:56
【问题描述】:

您能帮我吗,如何修改代码以获得包含 bin 计数的直方图,包括右 bin 边缘,即 bins[i-1] < x <= bins[i](默认情况下没有左侧)?

import matplotlib.pyplot as plt
import numpy as np
data = [0,1,2,3,4]
binwidth = 1
plt.hist(data, bins=np.arange(min(data), max(data) + binwidth, binwidth))
plt.xlabel('Time')
plt.ylabel('Counts')
plt.show()

【问题讨论】:

    标签: python matplotlib histogram


    【解决方案1】:

    我认为在 matplotlib 或 numpy 中都没有明确的选择。

    但是,您可以将 np.histogram() 与您的 data(和 bin)的负值一起使用,然后将输出取反并使用 plt.bar() 函数绘制它。

    bins = np.arange(min(data), max(data) + binwidth, binwidth)
    hist, binsHist = np.histogram(-data, bins=sorted(-bins))
    plt.plot(-binsHist[1:], -hist, np.diff(binHist))
    

    【讨论】:

    • 非常感谢 abukaj 的快速回答,尤其是代码!
    • @Lilly:如果你觉得这个答案有帮助,你可以考虑接受它。
    • @abukaj :对不起,我是新用户……不知道我必须接受它。完成!
    猜你喜欢
    • 1970-01-01
    • 2021-05-11
    • 2018-01-12
    • 2013-10-08
    • 1970-01-01
    • 2013-06-11
    • 1970-01-01
    • 2020-06-05
    • 1970-01-01
    相关资源
    最近更新 更多