python matplotlib之 hist直方图

来源:互联网 发布:韦德2016季后赛数据 编辑:程序博客网 时间:2024/06/05 17:10

例子如下:

# coding=utf-8import numpy as npfrom numpy.linalg import choleskyimport matplotlib.pyplot as pltsampleNo = 1000;# 一维正态分布# 下面三种方式是等效的mu = 3sigma = 0.1np.random.seed(0)s = np.random.normal(mu, sigma, sampleNo )#s = np.random.rand(1, sampleNo )plt.subplot(141)plt.hist(s, 10, normed=True)   #####bins=10np.random.seed(0)s = sigma * np.random.randn(sampleNo ) + muplt.subplot(142)plt.hist(s, 30, normed=True)   #####bins=30np.random.seed(0)s = sigma * np.random.standard_normal(sampleNo ) + muplt.subplot(143)plt.hist(s, 30, normed=True)   #####bins=30# 二维正态分布mu = np.array([[1, 5]])Sigma = np.array([[1, 0.5], [1.5, 3]])R = cholesky(Sigma)s = np.dot(np.random.randn(sampleNo, 2), R) + muplt.subplot(144)# 注意绘制的是散点图,而不是直方图plt.plot(s[:,0],s[:,1],'+')plt.show()
  • hist(x,bins) 函数中bins是指直方图的总个数,个数越多,条形带越紧密。

这里写图片描述