引言
針對不同的資料型別和資料任務,我們應該如何選擇合適的資料可視化?
本文整理了資料可視化的經典套路,希望對你有所啟發,
資料分類
首先,我們對資料型別進行分析,
基于任務分類學的資料型別(Data Type By Task Taxonomy, TTT)中將資料分為7類,即一維線性資料、二維資料、三維資料、多維資料、時間資料、樹型資料和網狀資料1,這七種資料型別所反映的是對現實的抽象,
其中一維資料、二維資料、三維資料、時間資料大家都很熟悉,這里不做贅述,
-
多維資料: 一般有多個屬性欄位,可以表示為高維空間的一個點,然后用三維散點圖進行可視化,
-
樹結構: 一般用來表達層次關系,是一種常用的非線性資料結構,
-
網狀資料/圖結構: 一般用來表達連接關系,也是一種常用的非線性資料結構,常用節點連接圖及連接矩陣進行表示,網狀資料(圖結構)常用來表現自然世界和社會關系中的包含和從屬關系、組織資訊和邏輯承接關系等,
?? 可視化方法選型
確定資料型別之后,根據常見的資料可視化需求,我們可以把可視化目標分為比較、關系、分布、組合四大類,
下圖總結了根據需求分析可采用的統計可視化方法,2

? 可視化之前的資料處理
在進行資料分析和可視化之前,通常要對復雜資料進行預處理,常見資料處理如下2,
-
合并:將兩個以上的屬性合并成一個屬性或物件,包括有效簡化資料、改變資料尺度,
-
采樣:采樣是統計學的基本方法,也是對資料進行選擇的主要手段,對資料的初步探索和最后的資料分析環節經常被采用,
-
降維:維度越高,資料集在維度空間的分布越稀疏,從而減弱了資料集的密度和距離的定義對資料聚類和離群值檢測等操作的影響,將資料屬性的維度降低,有助于解決維度災難,減少資料處理的時間和記憶體消耗,更為有效地可視化資料,降低噪聲或消除無關特征等,
-
特征子集選擇:從資料集中選擇部分資料屬性值可以消除冗余的特征、與任務無關的特征,包括暴力列舉法、特征重要性選擇、壓縮感知理論的稀疏表達方法,
-
特征生成:特征生成是指在原始資料的基礎上構建新的能反映資料集重要資訊的屬性,包括特征抽取、將資料應用到新空間、基于特征融合與特征變換的特征構造,
-
離散化與二值化:將資料集根據分布劃分為若干個子類,形成對資料集的離散表達,
-
屬性變換:將某個屬性的所有的可能值一一映射到另一個空間,如指數變換、取絕對值等,
? 常用可視化代碼(python)
資料預處理完成之后,資料可視化既可以自己編程實作,也可以借助現有的可視化工具,
下面整理了雙變數/多變數進行可視化分析的常用代碼,基于matplotlib和seaborn實作,3
資料概覽
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
df=sns.load_dataset('titanic')
# 查看前5條資料
df.head()
# 查看資料量
print(df.shape)
# 查看數值型變數的統計資訊,包括數量、均值、標準差、最大最小值、分位數
df.describe()
相關性圖
相關性圖可以反映兩個變數之間的相關方向,在繪圖中還需要增加相關系數,以更直觀地判斷相關程度,
from scipy.stats import pearsonr
sns.jointplot(x="pclass",y="age",data=df,kind="reg",stat_func=pearsonr)
熱力圖
熱力圖通過色塊的顏色、深淺來表示不同變數之間的相關性,
f=df[['age','fare','sibsp']].corr()
sns.heatmap(f,annot=True)
邊際直方圖
# Import Data
df = pd.read_csv("https://raw.githubusercontent.com/selva86/datasets/master/mpg_ggplot2.csv")
# Create Fig and gridspec
fig = plt.figure(figsize=(16, 10), dpi= 80)
grid = plt.GridSpec(4, 4, hspace=0.5, wspace=0.2)
# Define the axes
ax_main = fig.add_subplot(grid[:-1, :-1])
ax_right = fig.add_subplot(grid[:-1, -1], xticklabels=[], yticklabels=[])
ax_bottom = fig.add_subplot(grid[-1, 0:-1], xticklabels=[], yticklabels=[])
# Scatterplot on main ax
ax_main.scatter('displ', 'hwy', s=df.cty*4, c=df.manufacturer.astype('category').cat.codes, alpha=.9, data=df, cmap="tab10", edgecolors='gray', linewidths=.5)
# histogram on the right
ax_bottom.hist(df.displ, 40, histtype='stepfilled', orientation='vertical', color='deeppink')
ax_bottom.invert_yaxis()
# histogram in the bottom
ax_right.hist(df.hwy, 40, histtype='stepfilled', orientation='horizontal', color='deeppink')
# Decorations
ax_main.set(title='Scatterplot with Histograms \n displ vs hwy', xlabel='displ', ylabel='hwy')
ax_main.title.set_fontsize(20)
for item in ([ax_main.xaxis.label, ax_main.yaxis.label] + ax_main.get_xticklabels() + ax_main.get_yticklabels()):
item.set_fontsize(14)
xlabels = ax_main.get_xticks().tolist()
ax_main.set_xticklabels(xlabels)
plt.show()
成對圖
# Load Dataset
df = sns.load_dataset('iris')
# Plot
plt.figure(figsize=(10,8), dpi= 80)
sns.pairplot(df, kind="reg", hue="species")
plt.show()
密度曲線+直方圖
# Import Data
df = pd.read_csv("https://github.com/selva86/datasets/raw/master/mpg_ggplot2.csv")
# Draw Plot
plt.figure(figsize=(13,10), dpi= 80)
sns.distplot(df.loc[df['class'] == 'compact', "cty"], color="dodgerblue", label="Compact", hist_kws={'alpha':.7}, kde_kws={'linewidth':3})
sns.distplot(df.loc[df['class'] == 'suv', "cty"], color="orange", label="SUV", hist_kws={'alpha':.7}, kde_kws={'linewidth':3})
sns.distplot(df.loc[df['class'] == 'minivan', "cty"], color="g", label="minivan", hist_kws={'alpha':.7}, kde_kws={'linewidth':3})
plt.ylim(0, 0.35)
# Decoration
plt.title('Density Plot of City Mileage by Vehicle Type', fontsize=22)
plt.legend()
plt.show()
網路可視化
樹結構、圖結構的可視化,可以用networkx來實作4,
import matplotlib.pyplot as plt
import networkx as nx
G = nx.petersen_graph()
subax1 = plt.subplot(121)
nx.draw(G, with_labels=True, font_weight='bold')
subax2 = plt.subplot(122)
nx.draw_shell(G, nlist=[range(5, 10), range(5)], with_labels=True, font_weight='bold')

?? 常用可視化工具
除了自己編程實作,我們也可以借助成熟的可視化軟體,快速制作漂亮的圖表,
Microsoft Excel
Office Power Map示例
Microsoft Power Map for Excel可以在三維地球或自定義地圖上繪制地理和時態資料,顯示這些資料,并創建可以與其他人分享的視覺瀏覽,

ECharts
https://echarts.apache.org/zh/index.html
國產可視化庫,應用廣泛,免費,開源,

Tableau
https://www.tableau.com/
BI領域常用的可視化平臺,全球范圍應用很廣的一款商業軟體,

Visualization Free
https://www.visualizefree.com/
一款免費的可視化工具,可以通過拖放設計器構建互動式可視化,

👉 經典可視化案例
-
風、氣象、海洋狀況的全球地圖:https://earth.nullschool.net/zh-cn/
-
標簽云制作: https://tagul.com/
-
全球最牛的28個大資料可視化應用案例:http://www.open-open.com/news/view/154a034/
-
地理資訊可視化開源庫:http://mapv.baidu.com/
👉 小結
本文整理了資料可視化分析的整體思路,提供了常用代碼和可視化工具,
下次面對資料分析任務,就可以參考可視化方法選型中的思路,根據你的需求,選擇適當的圖表進行可視化,
如果想要更漂亮的展示效果,可以借助成熟的可視化工具,
如果這篇文章對你有用的話,歡迎一鍵三連支持下博主~
Shneiderman B . The Eyes Have It: A Task by Data Type Taxonomy for Information Visualizations. 2000. ??
《大資料可視化》電子工業出版社 ?? ??
https://www.machinelearningplus.com/plots/top-50-matplotlib-visualizations-the-master-plots-python/ ??
https://networkx.org/documentation/stable/tutorial.html ??
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/296842.html
標籤:其他
