iT邦幫忙

2021 iThome 鐵人賽

DAY 29
0
AI & Data

從資料庫到資料分析視覺化系列 第 29

{DAY 29} Seaborn

前言

Seaborn是比matplotlib功能更強大的繪圖套件

是建立在matplotlib的基礎之上

主要用來和matplolib搭配使用

這篇的內容專注在類別變數的視覺化

學習的來源是seaborn的官網

User guide and tutorial - seaborn 0.11.2 documentation

章節的安排也是按照裡面的tutorial安排

tutorial裡面教的很詳細

所以我決定用裡面教的方法搭配實際數據練習

而數據的部分我則是會依照要分析的數據種類

從kaggle上找到適合的資料庫進行分析

Kaggle: Your Machine Learning and Data Science Community

類別變數

這裡也是沿用前面關於學生成績的數據

在使用前先引入要使用的套件

引入seaborn

使用import seaborn as sns

並且設定圖片的樣式:sns.set(style="white")

指定要使用白色網格的樣式

import seaborn as sns
sns.set(style="white")

接下來引入其他要用到的套件

跟讀取資料進來

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv("StudentsPerformance.csv")
df.head()

  1. barplot.()依照類別變數繪圖,在default 的情況下是採用各類別的數值平均值

    現在假設要看出不同教育程度的數學成績差異

    barplot.()裡常見的參數有

    • x,y: 比較的類別和數據
    • ax: 是否有指定子圖
    • data:使用哪個資料
    • palette: 調色盤,改變default的配色,我個人蠻喜歡的配色有"Set3","Set2","Paired"
    #先創建畫布和子圖
    fig = plt.figure(figsize=(25,5)) 
    ax1=fig.add_subplot(121)
    # 利用.barplot()作圖
    sns.barplot(ax=ax1, x='parental level of education',y='math score',data=df,palette="Set3")
    

    若是現在想要再更細分,可以使用hue增加分類的依據

    如果我想再加上性別這個變量:hue="gender"

    fig = plt.figure(figsize=(25,5))
    ax1=fig.add_subplot(121)
    sns.barplot(ax=ax1, x='parental level of education',y='math score',hue="gender",data=df,palette="Set3")
    

    在上篇matplotlib試過利用"'parental level of education'"分組,但是那時候需要經過複雜的.groupby().dropna()

    在seaborn裡可以省去這些繁雜的步驟

    現在來看不同教育程度在數學成績的平均值上的分佈

    fig,ax= plt.subplots(figsize=(10,5))
    sns.barplot(ax=ax, x='parental level of education',y="math score",data=df,palette="Set2")
    

    現在還可以利用hue=()加上其他的比較依據

    假如要比較不同組別

    fig,ax= plt.subplots(figsize=(20,5))
    sns.barplot(ax=ax, x='parental level of education',y='math score',hue="race/ethnicity",data=df,palette="Set3")
    

    可以看到原本類別變數在matplotlib需要經過很多資料的處理才有辦法畫出圖

    在seaborn可以很輕鬆的繪製

  2. .countplot()

    這個是用來計算各個類別變數的加總數量,比較類別的數量差距

    假如現在想知道不同性別在不同的教育程度上的數量差別

    fig,ax= plt.subplots(figsize=(15,5))
    sns.countplot(x='parental level of education',hue="gender",data=df,palette="Set3")
    

    上圖可以很輕鬆地看出不同性別分別在不同教育程度上的統計數量差別

    1. catplot()

      則是另外一個萬能的繪圖函示

      通常後面會接kind=" ",引號內放置想要繪製的圖表類型

      常見的有下面幾種,想要更多類型的話可以去官網上查

      • "count"
      • "point"
      • "bar"
      • "swarm"
      • "box"
      • "violin"

      若是想接續『不同性別在不同的教育程度上的數量差別』

      但是想要讓男生和女生的數據分別畫在兩張圖上

      而且把午餐是否減免的變數加上去

      可以使用col=" "引號內放上想要分開的類別

      sns.catplot(x='parental level of education', hue="gender", col="lunch", data=df, kind="count",height=4, aspect=2,palette="Paired")
      

      利用height,aspect調整版面的配置

      用短短幾個參數就可以根據不同的類別下去繪圖看出其中的差別

      • catplot(kind="box")

        練習繪製不同教育程度在數學成績分布上的box plot

        sns.catplot(x="parental level of education", y="math score", kind="box", data=df,height=8,palette="Set3" )
        

      • catplot(kind="swarm")

        sns.catplot(x="parental level of education", y="reading score", kind="swarm", height=8, data=df)
        

小結

Seaborn可以節省大量在處理資料分類上的時間

但是每個函式的使用上

參數都不太一樣

所以在使用前可以先用shift+tab查看參數的使用方法

這篇只有介紹類別變數

後面就會接續介紹數值變數的繪圖


上一篇
{DAY 28} Matplotlib 繪圖2
下一篇
{Day30} 網路爬蟲
系列文
從資料庫到資料分析視覺化30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言