天天看點

Seaborn繪圖Seaborn繪圖一、介紹與安裝二、分布圖二、pairplot三、stripplot和swarmplot四、boxplot五、jointplot六、violinplot七、pointplot, bar八、factorplot九、heatmap十、時序繪圖Reference

Seaborn繪圖

  • 全部代碼:https://github.com/lawlite19/Blog-Back-Up/blob/master/code/seaborn_study.py
  • 個人部落格位址:http://lawlite.me/2017/06/14/Seaborn%E7%BB%98%E5%9B%BE/

一、介紹與安裝

1、介紹

  • 官網:http://seaborn.pydata.org/index.html
  • Github: https://github.com/mwaskom/seaborn
  • Seaborn

    其實是在matplotlib的基礎上進行了更進階的

    API

    封裝,進而使得作圖更加容易
  • 在大多數情況下使用

    seaborn

    就能做出很具有吸引力的圖,而使用

    matplotlib

    就能制作具有更多特色的圖。應該把

    Seaborn

    視為

    matplotlib

    的補充

2、安裝

  • 直接

    pip3 install seaborn

    即可

二、分布圖

1、distplot

  • 導入包
#-*- coding: utf-8 -*-
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
#%matplotlib inline  # 為了在jupyter notebook裡作圖,需要用到這個指令
           
  • 加載

    seaborn

    中的資料集:

    tips = sns.load_dataset('tips')

  • 分布圖
    • kde

      是高斯分布密度圖,繪圖在0-1之間
    • hist

      是否畫直方圖
    • rug

      在X軸上畫一些分布線
    • fit

      可以制定某個分布進行拟合
    • label

      legend時的值
    • axlabel

      制定橫軸的說明
sns.distplot(tips['total_bill'], bins=None, hist=True, kde=False, rug=True, fit=None, 
            hist_kws=None, kde_kws=None, rug_kws=None, 
            fit_kws=None, color=None, vertical=False, 
            norm_hist=False, axlabel=None, label=None, ax=None)
sns.plt.show()
           
  • 拟合分布
    • 這裡使用了gamma分布拟合
from scipy import stats
sns.distplot(tips.total_bill, fit=stats.gamma, kde=False)
sns.plt.show()
           
Seaborn繪圖Seaborn繪圖一、介紹與安裝二、分布圖二、pairplot三、stripplot和swarmplot四、boxplot五、jointplot六、violinplot七、pointplot, bar八、factorplot九、heatmap十、時序繪圖Reference

2、kdeplot

  • 高斯機率密度圖
    • data2

      可以是二維的分布
    • shade

      是否填充
    • kernel

      核函數,還有很多核函數,比如cos, biw等
    • cumulative

      累積的作圖,最後的值應該是接近1
    • gridsize

      多少個點估計
ax = sns.kdeplot(tips['total_bill'], data2=tips.tip, shade=False, vertical=False, 
                    kernel="gau", bw="scott", 
                    gridsize=, cut=, clip=None, 
                    legend=True, cumulative=False, 
                    shade_lowest=True, ax=None)
    sns.plt.show()
           
Seaborn繪圖Seaborn繪圖一、介紹與安裝二、分布圖二、pairplot三、stripplot和swarmplot四、boxplot五、jointplot六、violinplot七、pointplot, bar八、factorplot九、heatmap十、時序繪圖Reference

二、pairplot

1、兩兩作圖

  • iris

    為例
    • data

      : DataFrame格式的資料
    • hue

      : label類别對應的column name
    • vars

      : 指定feature的列名
    • kind

      : 作圖的方式,可以是reg或scatter
    • diag_kind

      : 對角線作圖的方式,可以是hist或kde
iris = sns.load_dataset('iris')
    g = sns.pairplot(iris, hue='species', hue_order=None, palette=None, 
                     vars=list(iris.columns[:-]), 
                     x_vars=None, y_vars=None, 
                     kind="reg", diag_kind="hist", 
                     markers=['o','s','D'], size=, aspect=, 
                     dropna=True, plot_kws=None, 
                     diag_kws=None, grid_kws=None)
    sns.plt.show()
           
Seaborn繪圖Seaborn繪圖一、介紹與安裝二、分布圖二、pairplot三、stripplot和swarmplot四、boxplot五、jointplot六、violinplot七、pointplot, bar八、factorplot九、heatmap十、時序繪圖Reference

三、stripplot和swarmplot

1、stripplot

  • tips為例,檢視每天的資料資訊
    • x

      : X軸資料
    • y

      : Y軸資料
    • hue

      : 區分不同種類資料的column name
    • data

      : DataFrame類型資料
    • jitter

      : 将資料分開點,防止重疊
tips = sns.load_dataset('tips')
    ax = sns.stripplot(x='day', y='total_bill', hue=None, data=tips, order=None, 
                      hue_order=None, jitter=True, 
                      split=False, orient=None, 
                      color=None, palette=None, size=, 
                      edgecolor="gray", linewidth=, 
                      ax=None)
           
Seaborn繪圖Seaborn繪圖一、介紹與安裝二、分布圖二、pairplot三、stripplot和swarmplot四、boxplot五、jointplot六、violinplot七、pointplot, bar八、factorplot九、heatmap十、時序繪圖Reference
  • 檢視關于性别消費的資訊
ax = sns.stripplot(x='sex', y='total_bill', hue='day', data=tips, order=None, 
                      hue_order=None, jitter=True, 
                      split=False, orient=None, 
                      color=None, palette=None, size=, 
                      edgecolor="gray", linewidth=, 
                      ax=None)
           
Seaborn繪圖Seaborn繪圖一、介紹與安裝二、分布圖二、pairplot三、stripplot和swarmplot四、boxplot五、jointplot六、violinplot七、pointplot, bar八、factorplot九、heatmap十、時序繪圖Reference

2、swarmplot

  • 與stripplot類似,隻是資料點不會重疊 (适合小資料量)
tips = sns.load_dataset('tips')
    ax = sns.swarmplot(x='sex', y='total_bill', hue='day', data=tips)
    sns.plt.show()
           
Seaborn繪圖Seaborn繪圖一、介紹與安裝二、分布圖二、pairplot三、stripplot和swarmplot四、boxplot五、jointplot六、violinplot七、pointplot, bar八、factorplot九、heatmap十、時序繪圖Reference

四、boxplot

1、boxplot示意圖

Seaborn繪圖Seaborn繪圖一、介紹與安裝二、分布圖二、pairplot三、stripplot和swarmplot四、boxplot五、jointplot六、violinplot七、pointplot, bar八、factorplot九、heatmap十、時序繪圖Reference
  • 函數
    • x, y

      :指定X軸,Y軸的columns name值
    • hue

      : 指定要區分的類别
tips = sns.load_dataset('tips')
    ax = sns.boxplot(x='day', y='total_bill', hue=None, data=tips, order=None, 
                    hue_order=None, orient=None, 
                    color=None, palette=None, 
                    saturation=, width=, 
                    fliersize=, linewidth=None, 
                    whis=, notch=False, ax=None)
    sns.plt.show()
           
Seaborn繪圖Seaborn繪圖一、介紹與安裝二、分布圖二、pairplot三、stripplot和swarmplot四、boxplot五、jointplot六、violinplot七、pointplot, bar八、factorplot九、heatmap十、時序繪圖Reference

- 可以和上面的stripplot一起用

tips = sns.load_dataset('tips')
    ax = sns.boxplot(x='day', y='total_bill', hue=None, data=tips, order=None, 
                    hue_order=None, orient=None, 
                    color=None, palette=None, 
                    saturation=, width=, 
                    fliersize=, linewidth=None, 
                    whis=, notch=False, ax=None)
    sns.stripplot(x='day', y='total_bill', hue=None, data=tips, order=None, 
                 hue_order=None, jitter=True, split=False, 
                 orient=None, color=None, palette=None, 
                 size=, edgecolor="gray", linewidth=, 
                 ax=None)
    sns.plt.show()
           
Seaborn繪圖Seaborn繪圖一、介紹與安裝二、分布圖二、pairplot三、stripplot和swarmplot四、boxplot五、jointplot六、violinplot七、pointplot, bar八、factorplot九、heatmap十、時序繪圖Reference

五、jointplot

1、jointplot

  • 聯合作圖
    • kind

      : 有scatter” | “reg” | “resid” | “kde” | “hex
tips = sns.load_dataset('tips')
    from scipy import stats
    g = sns.jointplot(x='total_bill', y='tip',
                      data=tips, kind="reg", 
                      stat_func=stats.pearsonr, 
                      color=None, size=, ratio=, 
                      space=, dropna=True, xlim=None, 
                      ylim=None, joint_kws=None, 
                      marginal_kws=None, annot_kws=None)
    sns.plt.show()
           
Seaborn繪圖Seaborn繪圖一、介紹與安裝二、分布圖二、pairplot三、stripplot和swarmplot四、boxplot五、jointplot六、violinplot七、pointplot, bar八、factorplot九、heatmap十、時序繪圖Reference
  • 可以在基礎上再作圖
    • plot_joint就是在聯合分布上作圖
    • plot_marginals就是在邊緣分布上再作圖
Seaborn繪圖Seaborn繪圖一、介紹與安裝二、分布圖二、pairplot三、stripplot和swarmplot四、boxplot五、jointplot六、violinplot七、pointplot, bar八、factorplot九、heatmap十、時序繪圖Reference

六、violinplot

1、小提琴圖,和boxplot很像

  • 對稱的kde圖
  • 中間的白點是中位數,黑色粗線對應分位數

Seaborn繪圖Seaborn繪圖一、介紹與安裝二、分布圖二、pairplot三、stripplot和swarmplot四、boxplot五、jointplot六、violinplot七、pointplot, bar八、factorplot九、heatmap十、時序繪圖Reference

  • inner

    : 指定圖裡面用什麼劃分,有

    "box", "quartile", "point", "stick", None

    • quartile

      為四分位數劃分
    • stick

      很像rug,就是可以看出密度情況
  • scale

    : 縮放每個圖對應的area, 取值有

    "area", "count", "width"

    • area

      指定每個有相同的area
    • count

      會按數量縮放(數量少的就比較窄扁)
tips = sns.load_dataset('tips')
    ax = sns.violinplot(x='day', y='total_bill', 
                        hue='smoker', data=tips, order=None, 
                        hue_order=None, bw="scott", 
                        cut=, scale="area", 
                        scale_hue=True, gridsize=, 
                        width=, inner="quartile", 
                        split=False, orient=None, 
                        linewidth=None, color=None, 
                        palette='muted', saturation=, 
                        ax=None) 
    sns.plt.show()
           
Seaborn繪圖Seaborn繪圖一、介紹與安裝二、分布圖二、pairplot三、stripplot和swarmplot四、boxplot五、jointplot六、violinplot七、pointplot, bar八、factorplot九、heatmap十、時序繪圖Reference

七、pointplot, bar

1、pointplot

  • 點圖
    • estimator

      :點的取值是,預設是

      np.mean

tips = sns.load_dataset('tips')
    sns.pointplot(x='time', y='total_bill', hue='smoker', data=tips, order=None, 
                 hue_order=None, estimator=np.mean, ci=, 
                 n_boot=, units=None, markers="o", 
                 linestyles="-", dodge=False, join=True, 
                 scale=, orient=None, color=None, 
                 palette=None, ax=None, errwidth=None, 
                 capsize=None)
    sns.plt.show()
           
Seaborn繪圖Seaborn繪圖一、介紹與安裝二、分布圖二、pairplot三、stripplot和swarmplot四、boxplot五、jointplot六、violinplot七、pointplot, bar八、factorplot九、heatmap十、時序繪圖Reference

2、barplot

  • 條形圖
    • y軸是

      mean value

      ,和點圖其實差不多
tips = sns.load_dataset('tips')
    sns.barplot(x='day', y='total_bill', hue='sex', data=tips, order=None, 
               hue_order=None, estimator=np.mean, ci=, 
               n_boot=, units=None, orient=None, 
               color=None, palette=None, saturation=, 
               errcolor=".26", errwidth=None, capsize=None, 
               ax=None)
    sns.plt.show()
           
Seaborn繪圖Seaborn繪圖一、介紹與安裝二、分布圖二、pairplot三、stripplot和swarmplot四、boxplot五、jointplot六、violinplot七、pointplot, bar八、factorplot九、heatmap十、時序繪圖Reference

3、countplot

tips = sns.load_dataset('tips')
    sns.countplot(x='day', hue='sex', data=tips)
    sns.plt.show()  
           
Seaborn繪圖Seaborn繪圖一、介紹與安裝二、分布圖二、pairplot三、stripplot和swarmplot四、boxplot五、jointplot六、violinplot七、pointplot, bar八、factorplot九、heatmap十、時序繪圖Reference

八、factorplot

1、可以通過這個函數繪制以上幾種圖

  • 指定

    kind

    即可,有

    point

    ,

    bar

    ,

    count

    ,

    box

    ,

    violin

    ,

    strip

  • row

    col

    指定繪制的行數和列數,給出一個種類類型的列名即可
titanic = sns.load_dataset('titanic')
    sns.factorplot(x='age', y='embark_town', 
                   hue='sex', data=titanic,
                   row='class', col='sex', 
                   col_wrap=None, estimator=np.mean, ci=, 
                   n_boot=, units=None, order=None, 
                   hue_order=None, row_order=None, 
                   col_order=None, kind="box", size=, 
                   aspect=, orient=None, color=None, 
                   palette=None, legend=True, 
                   legend_out=True, sharex=True, 
                   sharey=True, margin_titles=False, 
                   facet_kws=None)
    sns.plt.show()
           
Seaborn繪圖Seaborn繪圖一、介紹與安裝二、分布圖二、pairplot三、stripplot和swarmplot四、boxplot五、jointplot六、violinplot七、pointplot, bar八、factorplot九、heatmap十、時序繪圖Reference

九、heatmap

1、heatmap

flight = sns.load_dataset('flights')
    flights = flight.pivot('month','year','passengers')
    sns.heatmap(flights, annot=True, fmt='d')
    sns.plt.show()
           
Seaborn繪圖Seaborn繪圖一、介紹與安裝二、分布圖二、pairplot三、stripplot和swarmplot四、boxplot五、jointplot六、violinplot七、pointplot, bar八、factorplot九、heatmap十、時序繪圖Reference

十、時序繪圖

1、tsplot

  • condition: 和

    hue

    差不多,指定類别
  • estimator: 預設為

    np.mean

gammas = sns.load_dataset('gammas')
    sns.tsplot(data=gammas, time='timepoint', unit='subject', 
               condition='ROI', value='BOLD signal', 
               err_style="ci_band", ci=, interpolate=True, 
               color=None, estimator=np.mean, n_boot=, 
               err_palette=None, err_kws=None, legend=True, 
               ax=None)
    sns.plt.show()
           
Seaborn繪圖Seaborn繪圖一、介紹與安裝二、分布圖二、pairplot三、stripplot和swarmplot四、boxplot五、jointplot六、violinplot七、pointplot, bar八、factorplot九、heatmap十、時序繪圖Reference

Reference

  • Youtube: https://www.youtube.com/playlist?list=PLgJhDSE2ZLxYlhQx0UfVlnF1F7OWF-9rp
  • Github: https://github.com/knathanieltucker/seaborn-weird-parts