解密大数据课程作业-直方图

作者: 游遍星辰99 | 来源:发表于2017-03-12 18:41 被阅读69次
    
    
    #coding:utf-8
    %matplotlib inline
    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    from scipy import stats
    from matplotlib.font_manager import *  
    
    weight_data=pd.read_table('weight.txt') #读入文件
    weight_data.shape  #显示数据框的结构
    
    (80, 1)
    
    weight_data['weight'].mean() #求均值
    
    50.7
    
    weight_data['weight'].var() #方差
    
    39.27594936708859
    
    weight_data.columns
    
    Index(['weight'], dtype='object')
    
    
    myfont = FontProperties(fname='/Library/Fonts/Lantinghei.ttc')
    fig=plt.figure()
    x=weight_data['weight']
    ax=fig.add_subplot(111)
    numBins=20
    ax.hist(x,numBins,color='blue',alpha=0.8)
    #plt.rcParams['font.sans-serif'] = ['SimHei'] #指定默认字体,在我的系统中没有效果
    plt.title(u'体重直方图',fontproperties=myfont)
    plt.xlabel(u'横坐标',fontproperties=myfont)
    plt.ylabel(u'纵坐标',fontproperties=myfont)
    plt.show()
    
    Paste_Image.png
    df = pd.read_csv('AirPassengers.csv')
    
    df
    
    df['NumPassengers'].mean()
    
    280.2986111111111
    
    df['NumPassengers'].var()
    
    14391.917200854701
    
    df['NumPassengers'].min()
    
    104
    
    df['NumPassengers'].max()
    
    622
    
    bins=11
    data=df['NumPassengers']
    plt.hist(data, bins=bins,range=(100, 650), alpha=0.5)
    plt.title('NumPassengers')
    plt.show()
    
    Paste_Image.png

    从直方图上看,数据集2的分布相对均匀一些,但是从数据看,数据集2的标准差更大。用代码一步一步可以把图做出来,但是如何解读才是真正的数据分析能力,对我来说,这是更需要下功夫的地方。

    遇到的问题:
    使用 plt.rcParams['font.sans-serif'] = ['SimHei'] 指定中文字体在我的mac上没有作用,在字体册里搜索了一下,没有SimHei这种字体,我猜是这个原因?但matplotlib的字体也未必就是系统自带的,这个问题暂时先放着,下次找个windows里面的SimHei.ttf复制过来,看看有没有用。借鉴了 彻底解决matplotlib中文乱码问题中的方法,在系统中找到一个Lantinghei 字体文件,应该对应字体册中的兰亭黑,使用fontproperties属性指定。

    相关文章

      网友评论

      • 鱼心DrFish:挺好的!
        有一个小细节,就是做图的时候,横坐标和纵坐标的含义最好能说清楚,比如横轴是体重,纵轴是频数。

      本文标题:解密大数据课程作业-直方图

      本文链接:https://www.haomeiwen.com/subject/gccxnttx.html