[英]Getting information for bins in matplotlib histogram function
我正在使用matplotlib在python中繪制直方圖:
plt.hist(nparray, bins=10, label='hist')
是否可以打印包含所有箱櫃信息的數據框,例如每個箱櫃中的元素數量?
plt.hist
的返回值是:
返回:元組:(n,bins,patches)或([n0,n1,...],bin,[patches0,patches1,...])
所以你需要做的就是適當地捕獲返回值。 例如:
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.
該bins
返回其限定用於每個區間的邊緣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.