Python 3 直方图:如何使用 plt.hist() 获取计数和分箱,但不在屏幕上显示直方图?
Python 3 histogram: how to get counts and bins with plt.hist(), but without displaying the histogram in screen?
我有一组数据需要从中提取信息。最好的方法是通过直方图:我想获得一个
为此,我使用了函数 matplotlib.pyplot.hist(),它允许我提取计数 n 和 bins 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.
中工作
提前致谢。
您可以为此使用 numpy
(docs)。
import numpy as np
x = np.random.randint(0,10,(100))
n, bins = np.histogram(x, bins=10)
您可以使用 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.])
我有一组数据需要从中提取信息。最好的方法是通过直方图:我想获得一个 为此,我使用了函数 matplotlib.pyplot.hist(),它允许我提取计数 n 和 bins 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.
中工作提前致谢。
您可以为此使用 numpy
(docs)。
import numpy as np
x = np.random.randint(0,10,(100))
n, bins = np.histogram(x, bins=10)
您可以使用 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.])