0


【seaborn】sns.set() 绘图风格设置

目录

1. sns.set() 函数

  1. seaborn.set(context=‘notebook’,
  2. style=‘darkgrid’,
  3. palette=‘deep’,
  4. font=‘sans-serif’,
  5. font_scale=1,
  6. color_codes=True,
  7. rc=None)

从这个set()函数,可以看出,通过它我们可以设置背景色、风格、字型、字体等。

我们定义一个函数,这个函数主要是生成100个0到15的变量,然后用这个变量画出6条曲线。

  1. defsinplot():
  2. x = np.linspace(0,15,100)for i inrange(1,6):
  3. plt.plot(x, np.sin(x + i *.5)*(7- i))
  4. sns.set()
  5. sinplot()

在这里插入图片描述


那么,问题来了,有人会说,这个set()函数这么多参数,只要改变其中任意一个参数的值,绘图效果就会发生变化,那我们怎么知道哪种搭配是最佳效果呢,难道我们要一个个去测试吗?
当然不是,seaborn提供了5种默认的风格,我们在实际绘图中只要选择一种喜欢的风格就可以了,下面我们就看看这5种风格的用法及效果。


2. 参数 style 指定的5种默认风格

函数参数:seaborn.set_style(style=None, rc=None),这里style可选参数值有:darkgrid,whitegrid,dark,white,ticks,下面我们就通过设置不同的风格,看看每种风格的效果。

1) sns.set(style=‘white’)

  1. sns.set(style='white')

在这里插入图片描述

2) sns.set(style=‘whitegrid’)

  1. sns.set(style='whitegrid')

在这里插入图片描述

3) sns.set(style=‘darkgrid’)

  1. sns.set(style='darkgrid')

在这里插入图片描述

4) sns.set(style=‘dark’)

  1. sns.set(style='dark')

在这里插入图片描述

5) sns.set(style=‘ticks’)

  1. sns.set(style='ticks')

在这里插入图片描述

3. sns.despine() 函数,移除图像的上部和右侧的坐标轴

这个函数可以移除图像的上部和右侧的坐标轴,我们看看效果。

  1. x = np.linspace(0,15,100)
  2. y = np.sin(x +1)*5
  3. sns.set(style='ticks')
  4. plt.plot(x, y)
  5. sns.despine()

在这里插入图片描述
这里默认移除了上部和右侧的轴,当然我们也可以移除其他轴,只要将表示四个边的参数值改为true即可,下面是个这个函数的参数

  1. seaborn.despine(fig=None, ax=None, top=True, right=True, left=False, bottom=False, offset=None, trim=False)

其中offset表示偏离左侧轴的距离。

  1. x = np.linspace(0,15,100)
  2. y = np.sin(x +1)*5
  3. sns.set(style='ticks')
  4. plt.plot(x, y)
  5. sns.despine(top=True, right=True, left=False, bottom=False,offset=50)

在这里插入图片描述

4. 使用with打开某种风格

在matplotlib中我们已经学过了,在一个figure对象中,我们可以添加多个子图,那么如果不同的子图使用不同的风格,我们该如何做呢?
很简单,使用with 打开某种风格,然后在with下画的图都使用with打开的分格,我们来看看代码。

  1. x = np.linspace(0,15,100)
  2. y1 = np.sin(x +1)*1
  3. y2 = np.sin(x +3)*3
  4. y3 = np.sin(x +5)*5
  5. fig = plt.figure()with sns.axes_style("ticks"):
  6. ax1 = fig.add_subplot(221)
  7. plt.plot(x, y1)
  8. ax1 = fig.add_subplot(222)
  9. plt.plot(x, y2)
  10. fig.add_subplot(223)
  11. plt.plot(x, y3)

在这里插入图片描述

5. 参数 context 指定的4种默认风格

seaborn.set() 的 context 这个参数是用来设置 绘图背景参数的,它主要来影响标签、线条和其他元素的效果,但不会影响整体的风格,跟style有点区别。

当然,也可以使用函数

  1. seaborn.set_context(context=None, font_scale=1, rc=None)

指定,效果一样

context 的可选值有: notebook(默认),paper, talk, poster

  1. x = np.linspace(0,15,100)
  2. y = np.sin(x +1)*1
  3. fig = plt.figure()
  4. fig.add_subplot(221)
  5. sns.set(context='paper', style='ticks')
  6. plt.plot(x, y)
  7. fig.add_subplot(222)
  8. sns.set(context='notebook', style='ticks')
  9. plt.plot(x, y)
  10. fig.add_subplot(223)
  11. sns.set(context='talk', style='ticks')
  12. plt.plot(x, y)
  13. fig.add_subplot(224)
  14. sns.set(context='poster', style='ticks')
  15. plt.plot(x, y)

在这里插入图片描述

使用函数

  1. seaborn.set_context(context=None, font_scale=1, rc=None)
  1. x = np.linspace(0,15,100)
  2. y = np.sin(x +1)*5
  3. sns.set_context("notebook", font_scale=1.5, rc={"lines.linewidth":2.5})
  4. plt.figure(figsize=(6,4))
  5. plt.plot(x, y)

在这里插入图片描述


reference:

  • 参考文章

本文转载自: https://blog.csdn.net/weixin_37804469/article/details/125704480
版权归原作者 Enzo 想砸电脑 所有, 如有侵权,请联系我们删除。

“【seaborn】sns.set() 绘图风格设置”的评论:

还没有评论