目录

NumPy - 使用Matplotlib的直方图( Histogram Using Matplotlib)

NumPy有一个numpy.histogram()函数,它是数据频率分布的图形表示。 相等水平尺寸的矩形对应于称为bin类间隔和对应于频率的variable height

numpy.histogram()

numpy.histogram()函数将输入数组和bin作为两个参数。 bin数组中的连续元素充当每个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 

它将产生以下输出 -

[3 4 5 2 1]
[0 20 40 60 80 100]

plt()

Matplotlib可以将直方图的这种数字表示转换为图形。 pyplot子模块的plt() function将包含数据和bin数组的数组作为参数并转换为直方图。

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()

它应该产生以下输出 -

直方图
↑回到顶部↑
WIKI教程 @2018