Numpy 简明教程
NumPy - Histogram Using Matplotlib
NumPy 有一个 numpy.histogram() 函数,它是数据频率分布的图形表示。称为 bin 的类区间对应的水平大小相等的矩形和对应频率的 variable height 。
NumPy has a numpy.histogram() function that is a graphical representation of the frequency distribution of data. Rectangles of equal horizontal size corresponding to class interval called bin and variable height corresponding to frequency.
numpy.histogram()
numpy.histogram() 函数以输入数组和箱作为两个参数。箱数组中的连续元素作为每个箱的边界。
The numpy.histogram() function takes the input array and bins as two parameters. The successive elements in bin array act as the boundary of each bin.
import numpy as np
a = np.array([22,87,5,43,56,73,55,54,11,20,51,5,79,31,27])
np.histogram(a,bins = [0,20,40,60,80,100])
hist,bins = np.histogram(a,bins = [0,20,40,60,80,100])
print hist
print bins
它将生成如下输出:
It will produce the following output −
[3 4 5 2 1]
[0 20 40 60 80 100]
plt()
Matplotlib 可以将这些数值表示的直方图转换为图形。pyplot 子模块的 plt() function 以包含数据和箱数组的数组作为参数,并将其转换为直方图。
Matplotlib can convert this numeric representation of histogram into a graph. The plt() function of pyplot submodule takes the array containing the data and bin array as parameters and converts into a histogram.
from matplotlib import pyplot as plt
import numpy as np
a = np.array([22,87,5,43,56,73,55,54,11,20,51,5,79,31,27])
plt.hist(a, bins = [0,20,40,60,80,100])
plt.title("histogram")
plt.show()
它应该产生以下输出 −
It should produce the following output −