【Python】繪圖,頻次直方圖

頻次直方圖、數據區間劃分和分佈密度
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('seaborn-white')

data = np.random.randn(1000)
#最基本的頻次直方圖命令
plt.hist(data)

#調節具體參數
#bins調節橫座標分區個數,alpha參數用來設置透明度
plt.hist(data, bins=30, normed=True, alpha=0.5, histtype='stepfilled',
         color='steelblue', edgecolor='none'
 


#對不同的分佈特徵的樣本進行對比時,將histtype=‘stepfilled’與透明性設置參數alpha搭配使用效果好
x1 = np.random.normal(0, 0.8, 1000)
x2 = np.random.normal(-2, 1, 1000)
x3 = np.random.normal(3, 2, 1000)
kwargs = dict(histtype='stepfilled', alpha=0.3, normed=True, bins=40)
plt.hist(x1, **kwargs)
plt.hist(x2, **kwargs)
plt.hist(x3, **kwargs)

#如果只需要簡單的計算每段區間的樣本數,而並不想畫圖顯示它們,那麼可以直接用np.histogram()
counts, bin_edges = np.histogram(data, bins=50)
print(counts)

#輸出結果:
[  1   1   2   4   6   9  16  16  28  34  52  42  61  85 135 172 188 231
 295 315 343 386 383 400 401 364 325 319 279 240 195 147 136 100  80  69
  40  28  23  11   9   9   7   3   4   2   1   1   1   1]

 


二維頻次直方圖與數據區間劃分
plt.hist2d: 二維頻次直方圖
mean = [0, 0]
cov = [[1,1], [1,2]]
x, y = np.random.multivariate_normal(mean, cov, 10000).T
plt.hist2d(x, y, bins=30, cmap='Blues')
cb = plt.colorbar()
cb.set_label('counts in bin')
 


#如果只想計算各個區間的樣本數,可以使用np.histogram2d()
counts, xedges, yedges = np.histogram2d(x, y, bins=30)

plt.hexbin:六邊形區間劃分
plt.hexbin(x, y, gridsize=30, cmap='Blues')
cb = plt.colorbar(label='count in bin')

 

原文:https://blog.csdn.net/jasonzhoujx/article/details/81772846 
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章