下面是一个关于二项分布随机数的程序。在这段代码中,我不理解句子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')发布于 2017-04-19 04:36:22
你看过documentation for numpy.histogram了吗
此函数接受一些数据(此处为x)和一系列柱状图(此处为xgrid),并以元组(hx,xedge)的形式返回每个柱状图中的观测值以及每个柱状图的边值。
稍后,该脚本使用以下线条绘制观测值(hx)与每个面元的中心位置(计算为xcenter)之间的关系:
ax.plot(xcenter,hx,'ko-')https://stackoverflow.com/questions/43435393
复制相似问题