【问题标题】:Python 3 histogram: how to get counts and bins with plt.hist(), but without displaying the histogram in screen?Python 3 直方图:如何使用 plt.hist() 获取计数和 bin,但不在屏幕上显示直方图?
【发布时间】:2019-11-21 12:23:57
【问题描述】:

我有一组数据,我需要从中提取信息。最好的方法是通过直方图:我想获得一个 为此,我使用了函数 matplotlib.pyplot.hist(),它允许我提取计数 n 和 bin bins 的数量。我使用函数如下:

import matplotlib.pyplot as plt
import pickle

with open('variables/dataHistogram', 'rb') as f:
    data= pickle.load(f)

nBins = 10
n, bins, patches = hist(np.sort(data), nBins, rwidth=0.9, normed = False)

我可以成功检索数据。我唯一的问题是,使用这种方法,总是显示直方图。出于我的目的,我不希望它显示出来。有没有一种方法可以在不显示直方图的情况下提取数据?

作为附加信息,我正在使用 Spyder 运行我的代码,并且我正在使用 Python 3。

提前致谢。

【问题讨论】:

    标签: python histogram


    【解决方案1】:

    您可以使用numpy.histogram,而不是plt.hist。示例:

    >>> import numpy as np
    >>> x = np.random.randint(0, 5, size=10)
    >>> x
    array([3, 2, 0, 2, 1, 0, 2, 0, 0, 3])
    >>> counts, bin_edges = np.histogram(x, bins=3)
    >>> counts
    array([4, 1, 5])
    >>> bin_edges
    array([0., 1., 2., 3.])
    

    【讨论】:

      【解决方案2】:

      您可以为此使用numpy (docs)。

      import numpy as np
      
      x = np.random.randint(0,10,(100))
      
      n, bins = np.histogram(x, bins=10)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-17
        • 1970-01-01
        • 1970-01-01
        • 2019-04-16
        • 2018-06-10
        • 1970-01-01
        • 2013-10-08
        • 1970-01-01
        相关资源
        最近更新 更多