首页 > 其他分享 >seaborn详解

seaborn详解

时间:2023-02-06 19:32:50浏览次数:46  
标签:set seaborn axes show 详解 sns plt sinplot


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

seaborn详解_坐标轴


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

seaborn详解_重置_02

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

seaborn详解_坐标轴_03

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

seaborn详解_坐标轴_04

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

seaborn详解_重置_05

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

seaborn详解_seaborn_06

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

seaborn详解_函数返回_07

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

seaborn详解_seaborn_08

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

seaborn详解_重置_09

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

seaborn详解_函数返回_10


如果您想要定制 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()

seaborn详解_坐标轴_11

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

seaborn详解_重置_12

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

seaborn详解_重置_13

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

seaborn详解_坐标轴_14

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

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


seaborn详解_seaborn_15



标签:set,seaborn,axes,show,详解,sns,plt,sinplot
From: https://blog.51cto.com/u_15955675/6040310

相关文章

  • pannas详解
    #coding=utf-8'''pannas的函数作用;read_csv(文件的路径)读取文件<class'pandas.core.frame.DataFrame'>(read_csv返回的对象)对象的方法与属性:属......
  • 详解Spring AOP自定义可重复注解没有生效问题
    目录1.问题背景2.不啰嗦,上代码3.问题排查3.1是不是切点写得有问题,于是换成如下形式:3.2是不是使用的地方不是代理对象4.问题原因 1.问题背景工作中遇......
  • tensorflow中slim详解
    1.变量的定义 from__future__importabsolute_importfrom__future__importdivisionfrom__future__importprint_functionimporttensorflowastfslim=tf.contrib......
  • python3中zip详解
    描述zip()函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象......
  • 一文详解TensorFlow模型迁移及模型训练实操步骤
    摘要:本文介绍将TensorFlow网络模型迁移到昇腾AI平台,并执行训练的全流程。然后以TensorFlow1.15训练脚本为例,详细介绍了自动迁移、手工迁移以及模型训练的操作步骤。本文分......
  • 一文详解TensorFlow模型迁移及模型训练实操步骤
    摘要:本文介绍将TensorFlow网络模型迁移到昇腾AI平台,并执行训练的全流程。然后以TensorFlow1.15训练脚本为例,详细介绍了自动迁移、手工迁移以及模型训练的操作步骤。本文......
  • JVM垃圾回收机制,万字详解
    JVM垃圾回收机制jvm的基本组成虚拟机的组成所谓java能实现跨平台,是因为在不同平台上运行不同的虚拟机决定的,因此java文件的执行不直接在操作系统上执行,而是通过jvm虚拟机执......
  • Redis详解
    Redis配置ymlspring:redis:host:82.157.248.243#host地址port:6379#地址端口号password:#密码database:......
  • java注解与反射详解
    一、注解篇1.1、注解的基本概念注解:一种代码级别的说明,它是JDK1.5及以后版本引入的一个特性,与类、接口、枚举是在同一个层次;它可以声明在包、类、字段、方法、局部变量......
  • C++右值引用,移动语义与完美转发详解
    tags:C++Interview写在前面总结一下深入理解C++11这本书的第三章第三节,右值引用部分.文中全部代码可以参考我在GitHub上传的部分:​​Learn_C_Cpp/c++11-14/Depth_unde......