使用 matplotlib 自訂

Seaborn 資料視覺化中級

Chris Moffitt

Instructor

Matplotlib 的 Axes

  • 透過 matplotlibAxes 物件可進行大多數自訂
  • 可將 Axes 傳入 seaborn 函式
fig, ax = plt.subplots()
sns.histplot(df['Tuition'], ax=ax)
ax.set(xlabel='Tuition 2013-14')

座標軸範例圖

Seaborn 資料視覺化中級

進一步自訂

  • axes 物件支援多種常見自訂
fig, ax = plt.subplots()
sns.histplot(df['Tuition'], ax=ax)
ax.set(xlabel="Tuition 2013-14",
       ylabel="Distribution", xlim=(0, 50000), 
title="2013-14 Tuition and Fees Distribution")

更多座標軸範例

Seaborn 資料視覺化中級

結合圖表

  • 可以結合並設定多個圖表
fig, (ax0, ax1) = plt.subplots(nrows=1, ncols=2, 
                               sharey=True, figsize=(7,4))

sns.histplot(df['Tuition'], stat='density', ax=ax0)
sns.histplot(df.query('State == "MN"')['Tuition'], stat='density', ax=ax1)

ax1.set(xlabel='Tuition (MN)', xlim=(0, 70000))
ax1.axvline(x=20000, label='My Budget', linestyle='--')
ax1.legend()
Seaborn 資料視覺化中級

結合圖表

並排圖表

Seaborn 資料視覺化中級

一起來練習吧!

Seaborn 資料視覺化中級

Preparing Video For Download...