matplotlib画直方图(hist)

区分直方图与条形图:

  • 条形图是用条形的长度表示各类别频数的多少,其宽度(表示类别)则是固定的;
    直方图是用面积表示各组频数的多少,矩形的高度表示每一组的频数或频率,宽度则表示各组的组距,因此其高度与宽度均有意义。
  • 由于分组数据具有连续性,直方图的各矩形通常是连续排列,而条形图则是分开排列。
  • 条形图主要用于展示分类数据,而直方图则主要用于展示数据型数据

官方文档

程序与注释

#概率分布直方图

#高斯分布

#均值为0

mean = 0

#标准差为1,反应数据集中还是分散的值

sigma = 1

x=mean+sigma*np.random.randn(10000)

fig,(ax0,ax1) = plt.subplots(nrows=2,figsize=(9,6))
#The second parameter is the width of histogram
ax0.hist(x,40,normed=1,histtype='bar',facecolor='yellowgreen',alpha=0.75)
#pdf: Probability Density Function
ax0.set_title('pdf')
ax1.hist(x,20,normed=1,histtype='bar',facecolor='deepskyblue',alpha=0.75,cumulative=True,rwidth=1)
#cdf: Cumulative Distribution Function
ax1.set_title("cdf")

fig.subplots_adjust(hspace=0.4)
plt.show()
 

 

 

结果

matplotlib画直方图(hist)

pdf与cdf直方图