seaborn详解

# coding=utf-8


import seaborn as sns
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

def sinplot(flip=1):
    x = np.linspace(0, 14, 100)  #0~14分开产生100个数据
    for i in range(1, 7):
        plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip)



sinplot()
plt.show()


sns.set()
sinplot()
plt.show()

Seaborn 在底层将matplotlib 参数分成了两个独立的组。第一组设定了美学风格,第二组则是不同的数据元素,这样就可以很容易地添加到代码当中了。

为了控制风格,使用 axesstyle() 和 setstyle() 函数。为了绘图,请使用 plotting_context() 和 set_context() 函数。在这两种情况下,第一个函数返回一个参数字典,第二个函数则设置 matplotlib 默认属性。

风格控制:axes_style() and set_style()seaborn默认设置了5 个不同的主题,适用于不同的应用和人群偏好:

5种主题风格

  • darkgrid 黑色网格(默认)
    whitegrid 白色网格
    dark 黑色背景
    white 白色背景
    ticks 加上刻度的白色背景
sns.set_style("whitegrid")
data = np.random.normal(size=(20, 6)) + np.arange(6) / 2
sns.boxplot(data=data)
plt.show()

sns.set_style("dark")
sinplot()
plt.show()

sns.set_style("white")
sinplot()
plt.show()

sns.set_style("ticks")
sinplot()
plt.show()

sinplot()
sns.despine()  #去掉红色箭头标的线
plt.show()

sns.violinplot(data)
sns.despine(offset=10)
plt.show()

sns.set_style("whitegrid")
sns.boxplot(data=data, palette="deep")
sns.despine(left=True) # 删除左边边框   despine() 中添加参数去控制边框

seaborn.despine 函数的具体参数:
seaborn.despine(fig=None, ax=None, top=True, right=True, left=False, bottom=False, offset=None, trim=False)¶
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 None (default),
optional
Absolute distance, in
points, spines should be moved away from the axes (negative values move spines
inward).
trim : bool, optional
If true, limit spines to
the smallest and largest major tick on each non-despined axis.

临时设定图形样式

    虽然来回切换非常容易,但 sns (seaborn)也允许用 with 语句中套用 axes_style() 达到临时设置参数的效果(仅对 with 块内的绘图函数起作用)。这也允许创建不同风格的坐标轴。

with sns.axes_style("darkgrid"):
   plt.subplot(211) #subplot(211)与subplot(2,1,1)效果相同
   sinplot()
plt.subplot(212)
sinplot(-1)
plt.show()


如果您想要定制 seanborn 的样式,可以将参数字典传递给 axes_style() 和 set_style() 的 rc 参数。注意,只能通过该方法覆盖样式定义的一部分参数。(然而,更高层次的 set() 函数接受任何 matplotlib 参数的字典)。

如果您想要查看包含哪些参数,您可以只调用该函数而不带参数,这将返回当前设置的字典:

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}
sns.set_style("darkgrid", {"axes.facecolor": ".9"})
sinplot()
plt.show()

通过 plotting_context() 和set_context() 调整绘图元素

另一组参数控制绘图元素的大小,通过使用相同的代码来制作更合适大小的图形。

首先,可以通过 sns.set() 重置参数。

sns.set()

四种预设,按相对尺寸的顺序 (线条越来越粗),分别是 paper,notebook, talk, and poster。notebook 的样式是默认的,上面的绘图都是使用默认的 notebook 预设。

sns.set_context("paper")
plt.figure(figsize=(8, 6))
sinplot()
plt.show()

sns.set_context("talk")
plt.figure(figsize=(8, 6))
sinplot()
plt.show()

sns.set_context("poster")
plt.figure(figsize=(8, 6))
sinplot()
plt.show()

对于set_context() 参数,可以通过提供一个字典,来覆盖原来的参数 ,通过 context 的参数font_scale还可以改变字体元素的大小。(这个选项也可以通过顶级 set() 函数获得)。

sns.set_context("notebook", font_scale=1.5, rc={"lines.linewidth": 2.5})  #lines.linewidth代表线条的粗细
sinplot()


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值