hist的使用

来源:互联网 发布:线下数据采集 编辑:程序博客网 时间:2024/06/06 21:23

hist的api参数很多,如果要知道每个含义得一个一个去试,看了doc,这个hist的参数挺多的,api中有个简明的例子,我们使用几个重要的参数即可

函数签名

 

[html] view plain copy
  1. matplotlib.pyplot.hist(  
  2. x, bins=10range=Nonenormed=False,   
  3. weights=Nonecumulative=Falsebottom=None,   
  4. histtype=u'bar'align=u'mid'orientation=u'vertical',   
  5. rwidth=Nonelog=Falsecolor=Nonelabel=Nonestacked=False,   
  6. hold=None, **kwargs)  

 

x : (n,) array or sequence of (n,) arrays

这个参数是指定每个bin(箱子)分布的数据,对应x轴

bins : integer or array_like, optional

这个参数指定bin(箱子)的个数,也就是总共有几条条状图

normed : boolean, optional

If True, the first element of the return tuple will be the counts normalized to form a probability density, i.e.,n/(len(x)`dbin)

这个参数指定密度,也就是每个条状图的占比例比,默认为1

color : color or array_like of colors or None, optional

这个指定条状图的颜色

我们绘制一个10000个数据的分布条状图,共50份,以统计10000分的分布情况


%pylab inlineimport scipy.stats.stats as stfrom scipy.stats import normx_norm = norm.rvs(size=500)h = hist(x_norm)print 'counts, ', h[0]print 'bin centers', h[1]

这是一个很简单的例子,还有另一个例子。

  1. """  
  2. Demo of the histogram (hist) function with a few features.  
  3.   
  4. In addition to the basic histogram, this demo shows a few optional features:  
  5.   
  6.     * Setting the number of data bins  
  7.     * The ``normed`` flag, which normalizes bin heights so that the integral of  
  8.       the histogram is 1. The resulting histogram is a probability density.  
  9.     * Setting the face color of the bars  
  10.     * Setting the opacity (alpha value).  
  11.   
  12. """  
  13. import numpy as np  
  14. import matplotlib.mlab as mlab  
  15. import matplotlib.pyplot as plt  
  16.   
  17.   
  18. # example data  
  19. mu = 100 # mean of distribution  
  20. sigma = 15 # standard deviation of distribution  
  21. x = mu + sigma * np.random.randn(10000)  
  22.   
  23. num_bins = 50  
  24. # the histogram of the data  
  25. n, bins, patches = plt.hist(x, num_bins, normed=1facecolor='blue'alpha=0.5)  
  26. # add a 'best fit' line  
  27. y = mlab.normpdf(bins, mu, sigma)  
  28. plt.plot(bins, y, 'r--')  
  29. plt.xlabel('Smarts')  
  30. plt.ylabel('Probability')  
  31. plt.title(r'Histogram of IQ: $\mu=100$, $\sigma=15$')  
  32.   
  33. # Tweak spacing to prevent clipping of ylabel  
  34. plt.subplots_adjust(left=0.15)  
  35. plt.show()  


参考:http://blog.csdn.net/u013571243/article/details/48998619
0 0
原创粉丝点击