seaborn可视化之heatmap & time series & regression

之前尝试了用seaborn去做category和distribution可视化。时间序列的数据也是数据分析&挖掘的常客,这次选取了1965-2016全球重大地震数据做一些可视化及分析。主要研究下seaborn中heatmap,time series 以及regression function的使用。

seaborn中的plot function:

* heatmap: 用颜色矩阵去显示数据在两个维度下的度量值    
* tsplot: 用时间维度序列去展现数据的不确定性    
* regplot: 用线性回归模型对数据做拟合
* residplot: 展示线性回归模型拟合后各点对应的残值. 
ls ../input/earthquake/
earthquake@  earthquake.csv*
cd ../input/earthquake/
/kesci/data/earthquake
import pandas as pd
import numpy as np
data = pd.read_csv('earthquake.csv')
data.head()
 DateTimeLatitudeLongitudeTypeDepthDepth ErrorDepth Seismic StationsMagnitudeMagnitude Type...Magnitude Seismic StationsAzimuthal GapHorizontal DistanceHorizontal ErrorRoot Mean SquareIDSourceLocation SourceMagnitude SourceStatus
001/02/196513:44:1819.246145.616Earthquake131.6NaNNaN6.0MW...NaNNaNNaNNaNNaNISCGEM860706ISCGEMISCGEMISCGEMAutomatic
101/04/196511:29:491.863127.352Earthquake80.0NaNNaN5.8MW...NaNNaNNaNNaNNaNISCGEM860737ISCGEMISCGEMISCGEMAutomatic
201/05/196518:05:58-20.579-173.972Earthquake20.0NaNNaN6.2MW...NaNNaNNaNNaNNaNISCGEM860762ISCGEMISCGEMISCGEMAutomatic
301/08/196518:49:43-59.076-23.557Earthquake15.0NaNNaN5.8MW...NaNNaNNaNNaNNaNISCGEM860856ISCGEMISCGEMISCGEMAutomatic
401/09/196513:32:5011.938126.427Earthquake15.0NaNNaN5.8MW...NaNNaNNaNNaNNaNISCGEM860890ISCGEMISCGEMISCGEMAutomatic

5 rows × 21 columns

data['Date'] = pd.to_datetime(data['Date'])
data['Year'] = data['Date'].dt.year
data['Month'] = data['Date'].dt.month
data = data[data['Type'] == 'Earthquake']

先尝试用countplot作图,看看到底每年有多少地震发生

import warnings
warnings.filterwarnings("ignore")
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
plt.figure(1,figsize=(12,6))
Year = [i for i in range(1965,2017,5)]
idx = [i for i in range(0,52,5)]
sns.countplot(data['Year'])
plt.setp(plt.xticks(idx,Year)[1],rotation=45)
plt.title('Earthquake counts in history from year 1965 to year 2016')
plt.show()

作热力图heatmap去看看近十年来的地震记录
热力图的特点就在于定义两个具有意义的dimension,然后看数据在这两个dimension下的统计情况,完成对比分析。比如在下图中,我们将近十年来的地震记录数按照年份和月份做热力图。

test = data.groupby([data['Year'],data['Month']],as_index=False).count()
new = test[['Year','Month','ID']]
temp = new.iloc[-120:,:]
temp = temp.pivot('Year','Month','ID')
sns.heatmap(temp)
plt.title('Earthquake happened in recent 10 years')
plt.show()

做时间序列图去探究以年为单位,地震记录的趋势

temp = data.groupby('Year',as_index=False).count()
temp = temp.loc[:,['Year','ID']]
plt.figure(1,figsize=(12,6))
sns.tsplot(temp.ID,temp.Year,color="r")
plt.show()

对以年为单位的地震记录作线性回归拟合

plt.figure(figsize=(12,6))
plt.subplot(121)
sns.regplot(x="Year", y="ID", data=temp,order=1) # default by 1
plt.ylabel(' ')
plt.title('Regression fit of earthquake records by year,order = 1')

plt.subplot(122)
sns.residplot(x="Year", y="ID", data=temp)
plt.ylabel(' ')
plt.title('Residual plot when using a simplt regression model,order=1')
plt.show()

上方分别是对地震记录以年为单位统计之后的一阶线性回归拟合以及拟合后残值分布的情况。同样的思路,可以尝试高阶拟合,如下图的二阶拟合。

plt.figure(figsize=(12,6))
plt.subplot(121)
sns.regplot(x="Year", y="ID", data=temp,order=2) # default by 1
plt.ylabel(' ')
plt.title('Regression fit of earthquake records by year,order = 2')

plt.subplot(122)
sns.residplot(x="Year", y="ID", data=temp,order=2)
plt.ylabel(' ')
plt.title('Residual plot when using a simplt regression model,order=2')

plt.show()

同样的尝试,我们可以对地震记录中的深度Depth和强度Magnitude做线性拟合。

plt.figure(figsize=(12,6))
plt.subplot(121)
sns.regplot(x="Year", y="Depth", data=data,x_jitter=.05, x_estimator=np.mean,order=3)
  # x_estimator是一个参数,相当于对每年地震记录中参数取平均值,探究平均值的趋势
plt.ylabel(' ')
plt.title('Regression fit of depth,order = 3')

plt.subplot(122)
sns.regplot(x="Year", y="Magnitude", data=data,x_jitter=.05, x_estimator=np.mean,order=3)
    # x_estimator是一个参数,相当于对每年地震记录中参数取平均值,探究平均值的趋势
plt.title('Regression fit of magnitude,order=3')
plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值