matplot画图详解

所有的图的画法见https://matplotlib.org/ 官网,里面有各种图的示例。

1、直方图绘图:

plt.hist:

参数设置:

x: 指定每个bin(组)分布的数据,对应x轴

bins: (num_bins) 总共有几条条状图

color:颜色

density:如果为True,则返回元组的第一个元素将是规范化以形成概率密度的计数,即直方图下的面积(或积分)将总和为1.这是通过将计数除以观察次数来实现的。

    # example data  
    mu = 100 # mean of distribution  
    sigma = 15 # standard deviation of distribution  
    x = mu + sigma * np.random.randn(10000)  #生成均值是100方差是15的数据,作为x轴
      
    num_bins = 50  # 条状图的个数
    # the histogram of the data  
    n, bins, patches = plt.hist(x, num_bins, normed=1, facecolor='blue', alpha=0.5)  
    # add a 'best fit' line  
    y = mlab.normpdf(bins, mu, sigma)  
    plt.plot(bins, y, 'r--')  #画出分布曲线
    plt.xlabel('Smarts')  
    plt.ylabel('Probability')  
    plt.title(r'Histogram of IQ: $\mu=100$, $\sigma=15$')  

 直方图的统计学意义需要注意一下,一般而言,想到的是中学学到的身高情况的分布,体重,体育测试成绩的分布等等,某个区间段到某个区间段的人数是多少,通过直方图的形式可以很容易的看出来,现在重新审视这种表现方式,如果将横坐标看作是连续的,那么是有一种概率密度的意味的。

posted @ 2019-05-08 13:26  阿刚的代码进阶之旅  阅读(799)  评论(0编辑  收藏  举报