不使用 plt 从直方图中获取值

use*_*259 3 python histogram

有没有办法在不绘制**的情况下获取直方图的“x”和“y”中的值?我在我的代码中多次(在每个循环中)使用下面的函数,我注意到我的代码在每个循环中变得越来越慢。

** 我不确定它在内部所做的是否是绘制图形,但我知道尽管使用了 plt.close(),但我的代码中的缓慢与函数“plt.hist”有关。谢谢你。

# a is a list
def function_hist(a, ini, final):

    # 12 bins
    bins = np.linspace(ini, final, 13)
    weightsa = np.ones_like(a)/float(len(a))
    y, x, _ = plt.hist(a, bins, weights = weightsa)
    plt.close()
Run Code Online (Sandbox Code Playgroud)

Uma*_*pta 8

使用numpy.histogram

您可以修改您的功能如下

# a is a list
def function_hist(a, ini, final):

    # 12 bins
    bins = np.linspace(ini, final, 13)
    weightsa = np.ones_like(a)/float(len(a))
    hist = np.histogram(np.array(a), bins, weights = weightsa)
Run Code Online (Sandbox Code Playgroud)