Seaborn 介紹
Seaborn 基于 Matplotlib 核心庫進(jìn)行了更高階的 API 封裝寿弱,可以讓你輕松地畫出更漂亮的圖形焕梅。Seaborn 的漂亮主要體現(xiàn)在配色更加舒服精钮、以及圖形元素的樣式更加細(xì)膩攻泼,下面是 Seaborn 官方給出的參考圖胖秒。
Seaborn 具有如下特點(diǎn):
內(nèi)置數(shù)個經(jīng)過優(yōu)化的樣式效果雷厂。
增加調(diào)色板工具凤藏,可以很方便地為數(shù)據(jù)搭配顏色奸忽。
單變量和雙變量分布繪圖更為簡單,可用于對數(shù)據(jù)子集相互比較揖庄。
對獨(dú)立變量和相關(guān)變量進(jìn)行回歸擬合和可視化更加便捷栗菜。
對數(shù)據(jù)矩陣進(jìn)行可視化,并使用聚類算法進(jìn)行分析蹄梢。
基于時間序列的繪制和統(tǒng)計(jì)功能疙筹,更加靈活的不確定度估計(jì)。
基于網(wǎng)格繪制出更加復(fù)雜的圖像集合。
除此之外而咆, Seaborn 對 Matplotlib 和 Pandas 的數(shù)據(jù)結(jié)構(gòu)高度兼容霍比。
快速優(yōu)化圖形
當(dāng)我們使用 Matplotlib 繪圖時,默認(rèn)的圖像樣式算不上美觀暴备。此時桂塞,就可以使用 Seaborn 完成快速優(yōu)化。
import matplotlib.pyplot as plt
%matplotlib inline
x = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
y_bar = [3, 4, 6, 8, 9, 10, 9, 11, 7, 8]
y_line = [2, 3, 5, 7, 8, 9, 8, 10, 6, 7]
plt.bar(x, y_bar)
plt.plot(x, y_line, '-o', color='y')
import seaborn as sns
sns.set() # 聲明使用 Seaborn 樣式
plt.bar(x, y_bar)
plt.plot(x, y_line, '-o', color='y')
相比于 Matplotlib 默認(rèn)的純白色背景馍驯,Seaborn 默認(rèn)的淺灰色網(wǎng)格背景看起來的確要細(xì)膩舒適一些阁危。而柱狀圖的色調(diào)、坐標(biāo)軸的字體大小也都有一些變化汰瘫。
sns.set() 的默認(rèn)參數(shù)為:
sns.set(context='notebook', style='darkgrid', palette='deep', font='sans-serif', font_scale=1, color_codes=False, rc=None)
context='' 參數(shù)控制著默認(rèn)的畫幅大小狂打,分別有 {paper, notebook, talk, poster} 四個值。其中混弥,poster > talk > notebook > paper趴乡。
style='' 參數(shù)控制默認(rèn)樣式,分別有 {darkgrid, whitegrid, dark, white, ticks}蝗拿,你可以自行更改查看它們之間的不同晾捏。
palette='' 參數(shù)為預(yù)設(shè)的調(diào)色板。分別有 {deep, muted, bright, pastel, dark, colorblind} 等哀托,你可以自行更改查看它們之間的不同惦辛。
剩下的 font='' 用于設(shè)置字體,font_scale= 設(shè)置字體大小仓手,color_codes= 不使用調(diào)色板而采用先前的 'r' 等色彩縮寫胖齐。
Seaborn 繪圖 API
根據(jù)圖形的適應(yīng)場景,Seaborn 的繪圖方法大致分類 6 類嗽冒,分別是:關(guān)聯(lián)圖呀伙、類別圖、分布圖添坊、回歸圖剿另、矩陣圖和組合圖。而這 6 大類下面又包含不同數(shù)量的繪圖函數(shù)贬蛙。
關(guān)聯(lián)圖
relplot
是 relational plots 的縮寫雨女,其可以用于呈現(xiàn)數(shù)據(jù)之后的關(guān)系,主要有散點(diǎn)圖和條形圖 2 種樣式速客。本次實(shí)驗(yàn)戚篙,我們使用鳶尾花數(shù)據(jù)集進(jìn)行繪圖探索。在繪圖之前溺职,先熟悉一下 iris 鳶尾花數(shù)據(jù)集岔擂。數(shù)據(jù)集總共 150 行位喂,由 5 列組成。分別代表:萼片長度乱灵、萼片寬度塑崖、花瓣長度、花瓣寬度痛倚、花的類別规婆。其中,前四列均為數(shù)值型數(shù)據(jù)蝉稳,最后一列花的分類為三種抒蚜,分別是:Iris Setosa、Iris Versicolour耘戚、Iris Virginica嗡髓。
# 從國內(nèi)鏡像下載 seaborn 數(shù)據(jù)集避免下一步加載數(shù)據(jù)集失敗
!wget -nc "https://labfile.oss.aliyuncs.com/courses/2616/seaborn-data.zip"
!unzip seaborn-data.zip -d ~/
iris = sns.load_dataset("iris")
iris.head()
我們指定 x和 y的特征,默認(rèn)可以繪制出散點(diǎn)圖
sns.relplot(x="sepal_length", y="sepal_width", data=iris)
加入類別特征對數(shù)據(jù)進(jìn)行著色收津,就更好一些了
sns.relplot(x="sepal_length", y="sepal_width", hue="species", data=iris)
Seaborn 的函數(shù)都有大量實(shí)用的參數(shù)饿这,例如我們指定 style 參數(shù)可以賦予不同類別的散點(diǎn)不同的形狀。
sns.relplot(x="sepal_length", y="sepal_width",
hue="species", style="species", data=iris)
不只是散點(diǎn)圖撞秋,該方法還支持線形圖长捧,只需要指定 kind="line" 參數(shù)即可。線形圖和散點(diǎn)圖適用于不同類型的數(shù)據(jù)吻贿。線形態(tài)繪制時還會自動給出 95% 的置信區(qū)間串结。
sns.relplot(x="sepal_length", y="petal_length",
hue="species", style="species", kind="line", data=iris)
relplot 看作是 scatterplot 和 lineplot 的結(jié)合版本。
Seaborn 中的 API 分為 Figure-level 和 Axes-level 兩種廓八。relplot 就是一個 Figure-level 接口奉芦,而 scatterplot 和 lineplot 則是 Axes-level 接口赵抢。
Figure-level 和 Axes-level API 的區(qū)別在于剧蹂,Axes-level 的函數(shù)可以實(shí)現(xiàn)與 Matplotlib 更靈活和緊密的結(jié)合,而 Figure-level 則更像是「懶人函數(shù)」烦却,適合于快速應(yīng)用宠叼。
例如上方的圖,我們也可以使用 lineplot 函數(shù)繪制
sns.lineplot(x="sepal_length", y="petal_length",
hue="species", style="species", data=iris)
類別圖
類別圖的 Figure-level 接口是 catplot其爵,其為 categorical plots 的縮寫冒冬。而 catplot 實(shí)際上是如下 Axes-level 繪圖 API 的集合:
分類散點(diǎn)圖:
stripplot()
(kind="strip"
)
swarmplot()
(kind="swarm"
)
分類分布圖:
boxplot()
(kind="box"
)
violinplot()
(kind="violin"
)
boxenplot()
(kind="boxen"
)
分類估計(jì)圖:
pointplot()
(kind="point"
)
barplot()
(kind="bar"
)
countplot()
(kind="count"
)
catplot 繪圖效果,該方法默認(rèn)是繪制 kind="strip" 散點(diǎn)圖摩渺。
sns.catplot(x="sepal_length", y="species", data=iris)
kind="swarm" 可以讓散點(diǎn)按照 beeswarm 的方式防止重疊简烤,可以更好地觀測數(shù)據(jù)分布。
sns.catplot(x="sepal_length", y="species", kind="swarm", data=iris)
hue= 參數(shù)可以給圖像引入另一個維度摇幻,如果一個數(shù)據(jù)集有多個類別横侦,hue= 參數(shù)就可以讓數(shù)據(jù)點(diǎn)有更好的區(qū)分挥萌。
繪制箱線圖:
sns.catplot(x="sepal_length", y="species", kind="box", data=iris)
繪制小提琴圖:
sns.catplot(x="sepal_length", y="species", kind="violin", data=iris)
繪制增強(qiáng)箱線圖:
sns.catplot(x="species", y="sepal_length", kind="boxen", data=iris)
繪制點(diǎn)線圖:
sns.catplot(x="sepal_length", y="species", kind="point", data=iris)
繪制條形圖:
sns.catplot(x="sepal_length", y="species", kind="bar", data=iris)
繪制計(jì)數(shù)條形圖:
sns.catplot(x="species", kind="count", data=iris)
分布圖
分布圖主要是用于可視化變量的分布情況,一般分為單變量分布和多變量分布枉侧。當(dāng)然這里的多變量多指二元變量引瀑,更多的變量無法繪制出直觀的可視化圖形。
Seaborn 提供的分布圖繪制方法一般有這幾個: jointplot
榨馁,pairplot
憨栽,distplot
,kdeplot
翼虫。
Seaborn 快速查看單變量分布的方法是 distplot屑柔。默認(rèn)情況下,該方法將會繪制直方圖并擬合核密度估計(jì)圖珍剑。
sns.distplot(iris["sepal_length"])
distplot 提供了參數(shù)來調(diào)整直方圖和核密度估計(jì)圖锯蛀,例如設(shè)置 kde=False 則可以只繪制直方圖,或者 hist=False 只繪制核密度估計(jì)圖次慢。當(dāng)然旁涤,kdeplot 可以專門用于繪制核密度估計(jì)圖,其效果和 distplot(hist=False) 一致迫像,但 kdeplot 擁有更多的自定義設(shè)置劈愚。
sns.kdeplot(iris["sepal_length"])
jointplot 主要是用于繪制二元變量分布圖。例如闻妓,我們探尋 sepal_length 和 sepal_width 二元特征變量之間的關(guān)系菌羽。
sns.jointplot(x="sepal_length", y="sepal_width", data=iris)
jointplot 并不是一個 Figure-level 接口,但其支持 kind= 參數(shù)指定繪制出不同樣式的分布圖由缆。例如注祖,繪制出核密度估計(jì)對比圖。
sns.jointplot(x="sepal_length", y="sepal_width", data=iris, kind="kde")
六邊形計(jì)數(shù)圖:
sns.jointplot(x="sepal_length", y="sepal_width", data=iris, kind="hex")
回歸擬合圖:
sns.jointplot(x="sepal_length", y="sepal_width", data=iris, kind="reg")
pairplot 其支持一次性將數(shù)據(jù)集中的特征變量兩兩對比繪圖均唉。默認(rèn)情況下是晨,對角線上是單變量分布圖,而其他則是二元變量分布圖舔箭。
sns.pairplot(iris)
引入第三維度 hue="species" 會更加直觀罩缴。
sns.pairplot(iris, hue="species")
回歸圖
回歸圖的繪制函數(shù)主要有:lmplot
和 regplot
。
regplot 繪制回歸圖時层扶,只需要指定自變量和因變量即可箫章,regplot 會自動完成線性回歸擬合。
sns.regplot(x="sepal_length", y="sepal_width", data=iris)
lmplot 同樣是用于繪制回歸圖镜会,但 lmplot 支持引入第三維度進(jìn)行對比檬寂,例如我們設(shè)置 hue="species"。
sns.lmplot(x="sepal_length", y="sepal_width", hue="species", data=iris)
矩陣圖
矩陣圖中最常用的就只有 2 個戳表,分別是:heatmap
和 clustermap
桶至。
heatmap 主要用于繪制熱力圖拿诸。
import numpy as np
sns.heatmap(np.random.rand(10, 10))
熱力圖在某些場景下非常實(shí)用,例如繪制出變量相關(guān)性系數(shù)熱力圖塞茅。
clustermap
支持繪制 層次聚類 結(jié)構(gòu)圖亩码。如下所示,我們先去掉原數(shù)據(jù)集中最后一個目標(biāo)列野瘦,傳入特征數(shù)據(jù)即可描沟。
iris.pop("species")
sns.clustermap(iris)