sciPy stats.histogram()函数| Python
scipy.stats.histogram(a, numbins, defaultreallimits, weights, printextras)
将范围划分为几个 bin,然后返回每个 bin 中的实例数。此函数用于构建直方图。
Parameters :
arr : [array_like] input array.
numbins : [int] number of bins to use for the histogram. [Default = 10]
defaultlimits : (lower, upper) range of the histogram.
weights : [array_like] weights for each array element.
printextras : [array_like] to print the no, if extra points to the standard output, if true
Results :
– cumulative frequency binned values
– width of each bin
– lower real limit
– extra points.
代码#1:
# building the histogram
import scipy
import numpy as np
import matplotlib.pyplot as plt
hist, bin_edges = scipy.histogram([1, 1, 2, 2, 2, 2, 3],
bins = range(5))
# Checking the results
print ("No. of points in each bin : ", hist)
print ("Size of the bins : ", bin_edges)
# plotting the histogram
plt.bar(bin_edges[:-1], hist, width = 1)
plt.xlim(min(bin_edges), max(bin_edges))
plt.show()
输出 :
No. of points in each bin : [0 2 4 1]
Size of the bins : [0 1 2 3 4]