2017-04-16 21 views
0

下面是關於二項分佈隨機的程序。在這段代碼中,我不明白句子hx,xedge = np.histogram(x,xgrid)'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') 

回答

0

你看看documentation for numpy.histogram

這個函數有一些數據(這裏x),和倉(這裏xgrid)的序列,並返回觀測在每個倉的數量,以及每個區間的邊緣的值作爲一個元組(hx,xedge)

後來,腳本繪製觀測(hx)與各箱(如xcenter計算)的中心位置的數目,用線:

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

感謝。我知道文檔。但你的回答讓我更清楚。 –