Seaborn之繪圖風格設置

Seaborn簡介

seaborn同matplotlib一樣,也是Python進行數(shù)據(jù)可視化分析的重要第三方包。但seaborn是在 matplotlib的基礎上進行了更高級的API封裝,使得作圖更加容易,圖形更加漂亮。

seaborn并不能替代matplotlib。雖然seaborn可以滿足大部分情況下的數(shù)據(jù)分析需求,但是針對一些特殊情況,還是需要用到matplotlib的。換句話說,matplotlib更加靈活,可定制化,而seaborn像是更高級的封裝,使用方便快捷。

應該把seaborn視為matplotlib的補充,而不是替代物。

繪圖風格設置

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

首先,我們定義一個簡單的函數(shù)來繪制一些正弦波,用于測試

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()
seaborn1.png

轉換為seaborn默認繪圖,可以簡單的用set()方法。

import seaborn as sns

sns.set()
sinplot()
seaborn2.png

Seaborn 將 matplotlib 的參數(shù)劃分為兩個獨立的組合。第一組是設置繪圖的外觀風格的,第二組主要將繪圖的各種元素按比例縮放的,以至可以嵌入到不同的背景環(huán)境中。

操控這些參數(shù)的接口主要有兩對方法:

控制風格:axes_style(), set_style()
縮放繪圖:plotting_context(), set_context()
每對方法中的第一個方法(axes_style(), plotting_context())會返回一組字典參數(shù),而第二個方法(set_style(), set_context())會設置matplotlib的默認參數(shù)。

Seaborn的五種繪圖風格

有五種seaborn的風格,它們分別是:darkgrid, whitegrid, dark, white, ticks。它們各自適合不同的應用和個人喜好。默認的主題是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>
seaborn3.png
sns.set_style('dark')
sinplot()
seaborn4.png
sns.set_style('white')
sinplot()
seaborn5.png
sns.set_style('ticks')
sinplot()
seaborn6.png

移除軸脊柱

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

sinplot()
sns.despine()
seaborn7.png

一些繪圖也可以針對數(shù)據(jù)將軸脊柱進行偏置,當然也是通過調用despine()方法來完成。而當刻度沒有完全覆蓋整個軸的范圍時,trim參數(shù)可以用來限制已有脊柱的范圍。

f, ax = plt.subplots()
sns.violinplot(data=data)
sns.despine(offset=10, trim=True)
seaborn8.png

也可以通過despine()控制哪個脊柱將被移除。

sns.set_style("whitegrid")
sns.boxplot(data=data, palette="deep")
sns.despine(left=True)
seaborn9.png

臨時設置繪圖風格

雖然來回切換風格很容易,但是你也可以在一個with語句中使用axes_style()方法來臨時的設置繪圖參數(shù)。這也允許你用不同風格的軸來繪圖:

with sns.axes_style("darkgrid"):
    plt.subplot(211)
    sinplot()
plt.subplot(212)
sinplot(-1)
seaborn10.png

覆蓋seaborn風格元素

如果你想定制化seaborn風格,你可以將一個字典參數(shù)傳遞給axes_style()和set_style()的參數(shù)rc。而且你只能通過這個方法來覆蓋風格定義中的部分參數(shù)。

如果你想要看看這些參數(shù)都是些什么,可以調用這個方法,且無參數(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ù)的不同版本了。

sns.set_style("darkgrid",{'axes.facecolor':"0.9"})
sinplot()
seaborn11.png

繪圖元素比例

我們可以通過一套參數(shù)控制繪圖元素的比例
首先,我們通過set()方法重置默認的參數(shù)

sns.set()

有四個預置的環(huán)境,按大小從小到大排列分別為:paper, notebook, talk, poster。其中,notebook是默認的。

sns.set_context('paper')
sinplot()
seaborn12.png
sns.set_context('talk')
sinplot()
seaborn13.png
sns.set_context('poster')
sinplot()
seaborn14.png

我們可以通過set_context()方法設置相關參數(shù),并且你可以通過提供一個字典參數(shù)值來覆蓋參數(shù)。當改變環(huán)境時,你也可以獨立的去縮放字體元素的大小。

sns.set_context('notebook',font_scale = 1.5, rc = {'lines.linewidth':2.5})
sinplot()
seaborn15.png

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

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容