使用 FacetGrid、catplot 和 lmplot

Seaborn 資料視覺化中級

Chris Moffitt

Instructor

箱型圖 FacetGrid

Seaborn 資料視覺化中級

Tidy data(整潔資料)

  • Seaborn 的網格圖需要「tidy format」資料
  • 每列代表 1 筆觀測

整潔資料

Seaborn 資料視覺化中級

FacetGrid

  • FacetGrid 是許多資料感知網格的基礎
  • 你可控制資料如何分佈到欄、列與色相(hue
  • 建立 FacetGrid 後,需將圖形類型對應到網格
Seaborn 資料視覺化中級

FacetGrid 分類範例

g = sns.FacetGrid(df, col='HIGHDEG')
g.map(sns.boxplot, 'Tuition', 
      order=['1', '2', '3', '4'])

箱型圖網格範例

Seaborn 資料視覺化中級

catplot()

  • catplot 以更簡單方式在 FacetGrid 上畫分類資料
  • 將分面與對應合併為 1 個函式
sns.catplot(x="Tuition", data=df,
            col="HIGHDEG", kind="box")

箱型圖網格範例 2

Seaborn 資料視覺化中級

用 FacetGrid 畫迴歸

  • FacetGrid() 也可用於散佈圖或迴歸圖
g = sns.FacetGrid(df, col='HIGHDEG')
g.map(plt.scatter, 'Tuition', 'SAT_AVG_ALL')

散佈網格範例

Seaborn 資料視覺化中級

lmplot

  • lmplotFacetGrid 上繪製散佈與迴歸圖
sns.lmplot(data=df, x="Tuition", y="SAT_AVG_ALL",
           col="HIGHDEG", fit_reg=False)

散佈網格範例 2

Seaborn 資料視覺化中級

含迴歸的 lmplot

sns.lmplot(data=df, x="Tuition", y="SAT_AVG_ALL",
           col="HIGHDEG", row="REGION")

迴歸網格範例

Seaborn 資料視覺化中級

一起來練習吧!

Seaborn 資料視覺化中級

Preparing Video For Download...