#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屬性指定。