Python 在 matplotlib 直方图函数中获取 bin 的信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19442224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting information for bins in matplotlib histogram function
提问by dreamer_999
I am plotting a histogram in python using matplotlib by:
我正在使用 matplotlib 在 python 中绘制直方图:
plt.hist(nparray, bins=10, label='hist')
Is it possible to print a dataframe that has the information for all the bins, like number of elements in every bin?
是否可以打印包含所有 bin 信息的数据帧,例如每个 bin 中的元素数?
采纳答案by Bonlenfum
The return values of plt.hist
are:
的返回值plt.hist
是:
Returns: tuple : (n, bins, patches) or ([n0, n1, ...], bins, [patches0, patches1,...])
返回: 元组 : (n, bins, patch) 或 ([n0, n1, ...], bins, [patches0, patch1,...])
So all you need to do is capture the return values appropriately. For example:
因此,您需要做的就是适当地捕获返回值。例如:
import numpy as np
import matplotlib.pyplot as plt
# generate some uniformly distributed data
x = np.random.rand(1000)
# create the histogram
(n, bins, patches) = plt.hist(x, bins=10, label='hst')
plt.show()
# inspect the counts in each bin
In [4]: print n
[102 87 102 83 106 100 104 110 102 104]
# and we see that the bins are approximately uniformly filled.
# create a second histogram with more bins (but same input data)
(n2, bins2, patches) = plt.hist(x, bins=20, label='hst')
In [34]: print n2
[54 48 39 48 51 51 37 46 49 57 50 50 52 52 59 51 58 44 58 46]
# bins are uniformly filled but obviously with fewer in each bin.
The bins
that is returned defines the edges of each bin that was used.
的bins
返回,其限定用于每个区间的边缘。