Seaborn簡(jiǎn)介
seaborn同matplotlib一樣,也是Python進(jìn)行數(shù)據(jù)可視化分析的重要第三方包。但seaborn是在 matplotlib的基礎(chǔ)上進(jìn)行了更高級(jí)的API封裝,使得作圖更加容易,圖形更加漂亮。
seaborn并不能替代matplotlib。雖然seaborn可以滿足大部分情況下的數(shù)據(jù)分析需求,但是針對(duì)一些特殊情況,還是需要用到matplotlib的。換句話說,matplotlib更加靈活,可定制化,而seaborn像是更高級(jí)的封裝,使用方便快捷。
應(yīng)該把seaborn視為matplotlib的補(bǔ)充,而不是替代物。
繪圖風(fēng)格設(shè)置
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
首先,我們定義一個(gè)簡(jiǎn)單的函數(shù)來繪制一些正弦波,用于測(cè)試
def sinplot(flip = 1):
x = np.linspace(0, 14, 100)
for i in range(1, 7):
plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip)
sinplot()

轉(zhuǎn)換為seaborn默認(rèn)繪圖,可以簡(jiǎn)單的用set()方法。
import seaborn as sns
sns.set()
sinplot()

Seaborn 將 matplotlib 的參數(shù)劃分為兩個(gè)獨(dú)立的組合。第一組是設(shè)置繪圖的外觀風(fēng)格的,第二組主要將繪圖的各種元素按比例縮放的,以至可以嵌入到不同的背景環(huán)境中。
操控這些參數(shù)的接口主要有兩對(duì)方法:
控制風(fēng)格:axes_style(), set_style()
縮放繪圖:plotting_context(), set_context()
每對(duì)方法中的第一個(gè)方法(axes_style(), plotting_context())會(huì)返回一組字典參數(shù),而第二個(gè)方法(set_style(), set_context())會(huì)設(shè)置matplotlib的默認(rèn)參數(shù)。
Seaborn的五種繪圖風(fēng)格
有五種seaborn的風(fēng)格,它們分別是:darkgrid, whitegrid, dark, white, ticks。它們各自適合不同的應(yīng)用和個(gè)人喜好。默認(rèn)的主題是darkgrid。
sns.set_style('whitegrid')
data = np.random.normal(size = (20, 6)) + np.arange(6) / 2
sns.boxplot(data = data)
<matplotlib.axes._subplots.AxesSubplot at 0x1a1ae6da20>

sns.set_style('dark')
sinplot()

sns.set_style('white')
sinplot()

sns.set_style('ticks')
sinplot()

移除軸脊柱
white和ticks兩種風(fēng)格都可以移除頂部和右側(cè)的不必要的軸脊柱。使用matplotlib是無法實(shí)現(xiàn)這一需求的,但是使用seaborn的despine()方法可以實(shí)現(xiàn)。
sinplot()
sns.despine()

一些繪圖也可以針對(duì)數(shù)據(jù)將軸脊柱進(jìn)行偏置,當(dāng)然也是通過調(diào)用despine()方法來完成。而當(dāng)刻度沒有完全覆蓋整個(gè)軸的范圍時(shí),trim參數(shù)可以用來限制已有脊柱的范圍。
f, ax = plt.subplots()
sns.violinplot(data=data)
sns.despine(offset=10, trim=True)

也可以通過despine()控制哪個(gè)脊柱將被移除。
sns.set_style("whitegrid")
sns.boxplot(data=data, palette="deep")
sns.despine(left=True)

臨時(shí)設(shè)置繪圖風(fēng)格
雖然來回切換風(fēng)格很容易,但是你也可以在一個(gè)with語句中使用axes_style()方法來臨時(shí)的設(shè)置繪圖參數(shù)。這也允許你用不同風(fēng)格的軸來繪圖:
with sns.axes_style("darkgrid"):
plt.subplot(211)
sinplot()
plt.subplot(212)
sinplot(-1)

覆蓋seaborn風(fēng)格元素
如果你想定制化seaborn風(fēng)格,你可以將一個(gè)字典參數(shù)傳遞給axes_style()和set_style()的參數(shù)rc。而且你只能通過這個(gè)方法來覆蓋風(fēng)格定義中的部分參數(shù)。
如果你想要看看這些參數(shù)都是些什么,可以調(diào)用這個(gè)方法,且無參數(shù),這將會(huì)返回下面的設(shè)置:
sns.axes_style()
{'axes.facecolor': 'white',
'axes.edgecolor': '.8',
'axes.grid': True,
'axes.axisbelow': True,
'axes.labelcolor': '.15',
'figure.facecolor': 'white',
'grid.color': '.8',
'grid.linestyle': '-',
'text.color': '.15',
'xtick.color': '.15',
'ytick.color': '.15',
'xtick.direction': 'out',
'ytick.direction': 'out',
'lines.solid_capstyle': 'round',
'patch.edgecolor': 'w',
'image.cmap': 'rocket',
'font.family': ['sans-serif'],
'font.sans-serif': ['Arial',
'DejaVu Sans',
'Liberation Sans',
'Bitstream Vera Sans',
'sans-serif'],
'patch.force_edgecolor': True,
'xtick.bottom': False,
'xtick.top': False,
'ytick.left': False,
'ytick.right': False,
'axes.spines.left': True,
'axes.spines.bottom': True,
'axes.spines.right': True,
'axes.spines.top': True}
然后,就可以設(shè)置這些參數(shù)的不同版本了。
sns.set_style("darkgrid",{'axes.facecolor':"0.9"})
sinplot()

繪圖元素比例
我們可以通過一套參數(shù)控制繪圖元素的比例
首先,我們通過set()方法重置默認(rèn)的參數(shù)
sns.set()
有四個(gè)預(yù)置的環(huán)境,按大小從小到大排列分別為:paper, notebook, talk, poster。其中,notebook是默認(rèn)的。
sns.set_context('paper')
sinplot()

sns.set_context('talk')
sinplot()

sns.set_context('poster')
sinplot()

我們可以通過set_context()方法設(shè)置相關(guān)參數(shù),并且你可以通過提供一個(gè)字典參數(shù)值來覆蓋參數(shù)。當(dāng)改變環(huán)境時(shí),你也可以獨(dú)立的去縮放字體元素的大小。
sns.set_context('notebook',font_scale = 1.5, rc = {'lines.linewidth':2.5})
sinplot()

同樣也可以通過with語句控制繪圖的比例