Seaborn 入門

Seaborn 資料視覺化中級

Chris Moffitt

Instructor

Python 視覺化生態

  • Python 視覺化生態複雜,容易讓人不知從何下手

Python 視覺化生態

Seaborn 資料視覺化中級

Matplotlib

  • matplotlib 提供 Seaborn 視覺化的基礎元件
  • 也能獨立用來繪圖
import matplotlib.pyplot as plt
import pandas as pd

df = pd.read_csv("wines.csv")
fig, ax = plt.subplots() ax.hist(df['alcohol'])

Matplotlib 直方圖範例

Seaborn 資料視覺化中級

Pandas

  • pandas 是用來分析資料的基礎函式庫
  • 也支援基礎繪圖功能
import pandas as pd

df = pd.read_csv("wines.csv") df['alcohol'].plot.hist()

Pandas 直方圖範例

Seaborn 資料視覺化中級

Seaborn

  • Seaborn 支援更複雜的資料視覺化
  • 建立於 matplotlib 之上,最適合搭配 pandas 的 DataFrame 使用
Seaborn 資料視覺化中級

Seaborn histplot

  • histplot 類似前面範例的直方圖
  • 預設產生直方圖,也可繪製其他較複雜的圖
import seaborn as sns

sns.histplot(df['alcohol'])

Seaborn histplot 範例

Seaborn 資料視覺化中級

Seaborn displot

  • displot 整合 histplot 與其他函式來做分佈圖
  • 預設產生直方圖,也能切換成其他圖形
import seaborn as sns

sns.displot(df['alcohol'], kind='kde')

Seaborn displot 範例

Seaborn 資料視覺化中級

pandas 直方圖 vs. displot

  • Pandas 直方圖
    df['alcohol'].plot.hist()
    
    Pandas 直方圖範例
    • 顯示實際觀測頻率
    • 長條無外框
    • 分箱較寬
    • 無 x 軸標籤
  • Seaborn displot
    sns.displot(df['alcohol'])
    
    Seaborn displot 範例
    • 自動加上 x 軸標籤
    • 柔和色盤
    • 圖面更乾淨
Seaborn 資料視覺化中級

一起來練習吧!

Seaborn 資料視覺化中級

Preparing Video For Download...