Seaborn筆記一:設置圖像樣式及元素大小


Seaborn Tutorial

Introduction

在數據分析中,畫圖極為重要。在分析過程中以及最后的分析結果中都需要利用直觀的圖表去分析,展示數據特點,以便挖掘出有價值的信息。

因為我是用Python分析數據,那么matplotlib必不可少,然而自己太懶,matplotlib細節太多,一時沒法完全掌握。Seaborn看起來是個不錯的選擇,它基於matplotlib,
但是做了更高層次的封裝,學起來會簡單很多,所以我把它當做繪圖入門的工具。

我打算做一個Seaborn文檔的筆記,然后再附一兩篇實際應用的案例來完成我的Seaborn入門之旅。

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
plt.style.use({'figure.figsize':(12, 8)})

接下來畫一些正弦函數來看一下Seaborn的基本參數:

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()

看看matplotlib的樣式:

print(plt.style.available)
['bmh', 'classic', 'dark_background', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn-bright', 'seaborn-colorblind', 'seaborn-dark-palette', 'seaborn-dark', 'seaborn-darkgrid', 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook', 'seaborn-paper', 'seaborn-pastel', 'seaborn-poster', 'seaborn-talk', 'seaborn-ticks', 'seaborn-white', 'seaborn-whitegrid', 'seaborn', '_classic_test']

可以看到,Seaborn的樣式已經嵌入其中。我們將樣式設置為Seaborn:

sns.set()
sinplot()

png

Seaborn將matplotlib的參數分為兩個部分,一控制樣式,一控制圖中各種元素顯示的比例。

Seaborn figure styles

plt.style.available
['bmh',
 'classic',
 'dark_background',
 'fivethirtyeight',
 'ggplot',
 'grayscale',
 'seaborn-bright',
 'seaborn-colorblind',
 'seaborn-dark-palette',
 'seaborn-dark',
 'seaborn-darkgrid',
 'seaborn-deep',
 'seaborn-muted',
 'seaborn-notebook',
 'seaborn-paper',
 'seaborn-pastel',
 'seaborn-poster',
 'seaborn-talk',
 'seaborn-ticks',
 'seaborn-white',
 'seaborn-whitegrid',
 'seaborn',
 '_classic_test']

Matplotlib已經嵌入了 Seaborn樣式,嘗試一下不同的樣式:

data = np.random.normal(size=(20, 6))
sns.set_style('whitegrid')
sns.boxplot(data=data)
<matplotlib.axes._subplots.AxesSubplot at 0x1fbc03d6780>

png

sns.set_style('dark')
sinplot()

png

sns.set_style('white')
sinplot()

png

sns.set_style('ticks')
sinplot()

png

Remove axes spines

有時候我們會去掉box的上軸和右軸:

sinplot()
sns.despine()

png

當然你也可以:

sns.set_style('white')
sinplot()
sns.despine(left=True, bottom=True)

png

sns.despine()函數默認刪除右軸和上軸。

f, ax = plt.subplots()
sns.violinplot(data=data)
<matplotlib.axes._subplots.AxesSubplot at 0x1fbc05bdc18>

png

f, ax = plt.subplots()
sns.violinplot(data=data)
sns.despine(trim=True)

png

Temporarily setting figure style

我們可以臨時定義作圖參數讓子圖擁有不同的樣式:

with sns.axes_style('darkgrid'):
    plt.subplot(211)
    sinplot()
plt.subplot(212)
sinplot(-1)

png

這樣,我們就臨時調用了'darkgrid'樣式。

Overriding elements of the seaborn styles

我們可以看看Seaborn的默認樣式參數:

sns.axes_style()
{'axes.axisbelow': True,
 'axes.edgecolor': '.15',
 'axes.facecolor': 'white',
 'axes.grid': False,
 'axes.labelcolor': '.15',
 'axes.linewidth': 1.25,
 'figure.facecolor': 'white',
 'font.family': ['sans-serif'],
 'font.sans-serif': ['Arial',
  'Liberation Sans',
  'Bitstream Vera Sans',
  'sans-serif'],
 'grid.color': '.8',
 'grid.linestyle': '-',
 'image.cmap': 'Greys',
 'legend.frameon': False,
 'legend.numpoints': 1,
 'legend.scatterpoints': 1,
 'lines.solid_capstyle': 'round',
 'text.color': '.15',
 'xtick.color': '.15',
 'xtick.direction': 'out',
 'xtick.major.size': 0.0,
 'xtick.minor.size': 0.0,
 'ytick.color': '.15',
 'ytick.direction': 'out',
 'ytick.major.size': 0.0,
 'ytick.minor.size': 0.0}

它是一個字典,我們可以自己定義值:

sinplot()

png

sns.set_style({'axes.facecolor':'.9'})
sinplot()

png

其他參數也可以修改。

Scaling plot elements

接下來是修改圖的元素比例,包括線條大小,坐標大小等等。

sns.set()

Seaborn預設了四種比例,根據大小依次為 paper, notebook, talkposter。默認為 notebook

sns.plotting_context()
{'axes.labelsize': 11.0,
 'axes.titlesize': 12.0,
 'figure.figsize': [8.0, 5.5],
 'font.size': 12.0,
 'grid.linewidth': 1.0,
 'legend.fontsize': 10.0,
 'lines.linewidth': 1.75,
 'lines.markeredgewidth': 0.0,
 'lines.markersize': 7.0,
 'patch.linewidth': 0.3,
 'xtick.labelsize': 10.0,
 'xtick.major.pad': 7.0,
 'xtick.major.width': 1.0,
 'xtick.minor.width': 0.5,
 'ytick.labelsize': 10.0,
 'ytick.major.pad': 7.0,
 'ytick.major.width': 1.0,
 'ytick.minor.width': 0.5}

可以看到默認的元素大小。

sinplot()

png

sns.set_context('talk')
sinplot()

png

sns.set_context("poster")
sinplot()

png

同樣的,可以利用 sns.set_context()來設置sns.plotting_context()中的值。

zxzhu 2018/3/30


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM