seaborn可視化之heatmap & time series & regression

時間序列的數據也是數據分析&挖掘的常客,這次選取了1965-2016全球重大地震數據做一些可視化及分析,源代碼請查閱這裡。主要研究下seaborn中heatmap,time series 以及regression function的使用。

seaborn中的plot function:

  • heatmap: 用顏色矩陣去顯示數據在兩個維度下的度量值
  • tsplot: 用時間維度序列去展現數據的不確定性
  • regplot: 用線性回歸模型對數據做擬合
  • residplot: 展示線性回歸模型擬合後各點對應的殘值

數據集的讀取


先嘗試用countplot作圖,看看到底每年有多少地震發生

Year = [i for i in range(1965,2017,5)]nidx = [i for i in range(0,52,5)]nsns.countplot(data[Year])nplt.setp(plt.xticks(idx,Year)[1],rotation=45)nplt.title(Earthquake counts in history from year 1965 to year 2016)n


作熱力圖heatmap去看看近十年來的地震記錄

熱力圖的特點就在於定義兩個具有意義的dimension,然後看數據在這兩個dimension下的統計情況,完成對比分析。我們將近十年來的地震記錄數按照年份和月份做熱力圖。

test = data.groupby([data[Year],data[Month]],as_index=False).count()nnew = test[[Year,Month,ID]]ntemp = new.iloc[-120:,:]ntemp = temp.pivot(Year,Month,ID)nsns.heatmap(temp)n


做時間序列圖去探究以年為單位,地震記錄的趨勢

temp = data.groupby(Year,as_index=False).count()ntemp = temp.loc[:,[Year,ID]]nplt.figure(1,figsize=(12,6))nsns.tsplot(temp.ID,temp.Year,color="r")n


對以年為單位的地震記錄作線性回歸擬合

plt.figure(figsize=(12,6)) nplt.subplot(121) nsns.regplot(x="Year", y="ID", data=temp,order=1) # default by 1 nplt.title(Regression fit of earthquake records by year,order = 1)nplt.subplot(122)nsns.residplot(x="Year", y="ID", data=temp)nplt.ylabel( )nplt.title(Residual plot when using a simplt regression model,order=1)nplt.show()n

上方分別是對地震記錄以年為單位統計之後的一階線性回歸擬合以及擬合後殘值分布的情況。同樣的思路,可以嘗試高階擬合,如下圖的二階擬合。

同樣的嘗試,我們可以對地震記錄中的深度Depth和強度Magnitude做線性擬合。

plt.figure(figsize=(12,6))nplt.subplot(121)nsns.regplot(x="Year", y="Depth", data=data,x_jitter=.05, x_estimator=np.mean,order=3)n # x_estimator是一個參數,相當於對每年地震記錄中參數取平均值,探究平均值的趨勢nplt.ylabel( )nplt.title(Regression fit of depth,order = 3)nnplt.subplot(122)nsns.regplot(x="Year", y="Magnitude", data=data,x_jitter=.05, x_estimator=np.mean,order=3)n # x_estimator是一個參數,相當於對每年地震記錄中參數取平均值,探究平均值的趨勢nplt.title(Regression fit of magnitude,order=3)nplt.show()n

小結:

seaborn是個很強大的可視化package。一共寫了三篇筆記從不同的方向介紹seaborn中plot function的使用。其餘的內容請查閱科賽網以及知乎專欄Data Trek。

  • seaborn可視化學習之distribution visualization
  • seaborn可視化學習之category visualization

推薦閱讀:

新浪微博有哪些值得推薦的微博管理應用?
預測比特幣價格
第八章:回歸
索引和切片2

TAG:数据分析 | 数据可视化 | Python |