1、np.histogram
histogram(a,bins=10,range=None,weights=None,density=False);
- a是待统计数据的数组;
- bins指定统计的区间个数;
- range是一个长度为2的元组,表示统计范围的最小值和最大值,默认值None,表示范围由数据的范围决定
- weights为数组的每个元素指定了权值,histogram()会对区间中数组所对应的权值进行求和
- density为True时,返回每个区间的概率密度;为False,返回每个区间中元素的个数
a = np.random.rand(100)
hist,bins = np.histogram(a,bins=5,range=(0,1))
print(hist)
print(bins)
'''
out:
[19 30 15 16 20]
[ 0. 0.2 0.4 0.6 0.8 1. ]
'''
2、plt.hist
n,bins,patches=matplotlib.pyplot.hist(
x, bins=10, range=None, normed=False,
weights=None, cumulative=False, bottom=None,
histtype=u'bar', align=u'mid', orientation=u'vertical',
rwidth=None, log=False, color=None, label=None, stacked=False,
hold=None, **kwargs)
返回值:
- n:直方图向量,是否归一化由参数normed设定。
- 当normed取默认值时,n即为直方图各组内元素的数量(各组频数)
- bins: 返回各个bin的区间范围 patches:返回每个bin里面包含的数据,是一个list
参数
- x: 作直方图所要用的数据,必须是一维数组。多维数组可以先进行扁平化再作图
- bins: 直方图的柱数,可选项,默认为10
- normed: 是否将得到的直方图向量归一化。默认为0
- facecolor: 直方图颜色
- edgecolor: 直方图边框颜色
- alpha: 透明度
- histtype: 直方图类型,‘bar’, ‘barstacked’, ‘step’, ‘stepfilled’
(hist的参数非常多,但常用的有以下6个,只有第一个是必须的,后面5个可选)