'hx,xedge = np.histogram(x,xgrid)'的含义

时间:2017-04-16 08:42:07

标签: python numpy matplotlib histogram

以下是关于二项分布随机的程序。在此代码中,我不理解句子hx,xedge = np.histogram(x,xgrid)

它做什么?直方图用于绘制条形图吗?

我用这段代码制作折线图:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rc('xtick', labelsize=12)
matplotlib.rc('ytick', labelsize=12)
#generate random number from binomial density
# with test number of 10 and probability of event of 0.4
x = np.random.binomial(10,0.4,size=1000)
print(x)
#analyze the random samples with a histogram
xgrid = np.arange(0.5,20.5,1)
xcenter = (xgrid[1:]+xgrid[0:len(xgrid)-1])/2.
hx,xedge = np.histogram(x,xgrid)
#draw the histogram
fig = plt.figure(figsize=[10,5])
ax = fig.add_subplot(111)
ax.plot(xcenter,hx,'ko-')
fig.savefig('binomrand_hist.png',bbox_inches='tight')

1 个答案:

答案 0 :(得分:0)

您是否看过documentation for numpy.histogram

此函数获取一些数据(此处为x)和一系列二进制位(此处为xgrid),并返回每个二进制数中的观察数,以及边的值。每个bin都是一个元组(hx,xedge)

稍后,该脚本使用以下行描绘观察次数(hx)与每个箱位的中心位置(计算为xcenter):

ax.plot(xcenter,hx,'ko-')