数据可视化利器—— 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绘图风格


分享到:


相關文章: