綜合應用

Seaborn 資料視覺化入門

Content Team

DataCamp

開始上手

匯入 Seaborn:

import seaborn as sns

匯入 Matplotlib:

import matplotlib.pyplot as plt

顯示圖表:

plt.show()
Seaborn 資料視覺化入門

關係圖(Relational)

  • 顯示兩個連續變數的關係
  • 範例:散佈圖、折線圖
sns.relplot(x="x_variable_name", 
            y="y_variable_name", 
            data=pandas_df, 
            kind="scatter")
Seaborn 資料視覺化入門

類別圖(Categorical)

  • 在類別變數定義的類別中,顯示連續變數的分佈
  • 範例:長條圖、計數圖、盒鬚圖、點圖
sns.catplot(x="x_variable_name", 
            y="y_variable_name", 
            data=pandas_df, 
            kind="bar")
Seaborn 資料視覺化入門

加入第三個變數(hue)

設定 hue 會依子群著色,並在同一張圖上顯示。

帶有 hue 的散佈圖

1 Waskom, M. L. (2021). seaborn: statistical data visualization. https://seaborn.pydata.org/
Seaborn 資料視覺化入門

加入第三個變數(row/col)

relplot()catplot() 設定 row 與/或 col,可將子群分到不同子圖呈現。

帶有子圖的散佈圖

1 Waskom, M. L. (2021). seaborn: statistical data visualization. https://seaborn.pydata.org/
Seaborn 資料視覺化入門

自訂化

  • 改變背景:sns.set_style()
  • 改變主元素配色:sns.set_palette()
  • 調整尺度:sns.set_context()
Seaborn 資料視覺化入門

加入標題

Object Type Plot Types How to Add Title
FacetGrid relplot(), catplot() g.figure.suptitle()
AxesSubplot scatterplot(), countplot(), etc. g.set_title()
Seaborn 資料視覺化入門

最後修飾

新增 x、y 軸標籤:

g.set(xlabel="new x-axis label",
      ylabel="new y-axis label")

旋轉 x 刻度標籤:

plt.xticks(rotation=90)
Seaborn 資料視覺化入門

一起來練習吧!

Seaborn 資料視覺化入門

Preparing Video For Download...