數據可視化利器—— seaborn繪圖風格

關於seaborn的介紹,請參考我的前一篇文章:


我們直接進入正題,先從最基本的繪圖風格做起。

tips:seaborn內置了數據集tips,我們以後採用這個數據集來練習。

tips=sns.load_dataset('tips') #加載數據集

seaborn 與matplotlib的直觀對比

數據可視化利器—— seaborn繪圖風格

數據可視化利器—— seaborn繪圖風格

數據可視化利器—— seaborn繪圖風格

seaborn五種繪圖風格

有五種seaborn的風格,它們分別是:darkgrid, whitegrid, dark, white, ticks。它們各自適合不同的應用和個人喜好。默認的主題是darkgrid

。下面我們分別來看一下效果:

數據可視化利器—— seaborn繪圖風格

whitegrid

數據可視化利器—— seaborn繪圖風格

darkgrid

數據可視化利器—— seaborn繪圖風格

dark

數據可視化利器—— seaborn繪圖風格

white

數據可視化利器—— seaborn繪圖風格

ticks

軸脊柱控制

<code>sns.despine(
fig=None,
ax=None,
top=True, #指定TRUE代表去除
right=True,
left=False,
bottom=False,
offset=None,
trim=False,
)
Docstring:
Remove the top and right spines from plot(s).

fig : matplotlib figure, optional
Figure to despine all axes of, default uses current figure.
ax : matplotlib axes, optional
Specific axes object to despine.
top, right, left, bottom : boolean, optional
If True, remove that spine.
offset : int or dict, optional
Absolute distance, in points, spines should be moved away
from the axes (negative values move spines inward). A single value
applies to all spines; a dict can be used to set offset values per
side.
trim : bool, optional
If True, limit spines to the smallest and largest major tick
on each non-despined axis./<code>
數據可視化利器—— seaborn繪圖風格

數據可視化利器—— seaborn繪圖風格

sns.despine(offset=10,trim=True)

切換風格

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

數據可視化利器—— seaborn繪圖風格

seaborn風格元素大全

如果你想定製化seaborn風格,你可以將一個字典參數傳遞給axes_style()和set_style()的參數rc。而且你只能通過這個方法來覆蓋風格定義中的部分參數。如果你想要看看這些參數都是些什麼,可以調用這個方法,且無參數,這將會返回下面的設置:

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

seaborn四種繪圖背景

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

數據可視化利器—— seaborn繪圖風格

paper

數據可視化利器—— seaborn繪圖風格

talk

數據可視化利器—— seaborn繪圖風格

poster

當然,你也可以通過控制某些rc參數控制繪圖元素:

數據可視化利器—— seaborn繪圖風格


分享到:


相關文章: