Fork me on GitHub

seaborn画风和主题风格

本文中的主要知识点:

  • seaborn画风的使用
  • 怎么隐藏刻度线
  • 多个子图怎么使用不同的风格
  • 刻度轴上的数值大小和线条粗细设置
1
2
3
4
5
import seaborn as sns   # seaborn是对matplotlib的基础上进行了封装
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline

默认画风

1
2
3
4
def sinplot(flip=1):
x = np.linspace(0, 14, 100)
for i in range(1,7):
plt.plot(x, np.sin(x + i * .5)* (7 - i)* flip)
1
sinplot()  # 默认画风

1
2
sns.set()
sinplot()

png

5种主题风格

  • darkgrid
  • whitegrid
  • dark
  • white
  • ticks
1
2
3
sns.set_style("whitegrid")   # 风格常用
data = np.random.normal(size=(20, 6)) + np.arange(6) / 2
sns.boxplot(data=data)
<matplotlib.axes._subplots.AxesSubplot at 0x19234fef048>

png

1
2
sns.set_style('dark')   # 去掉默认的格子和刻度线
sinplot()

png

1
2
sns.set_style('white')  # 背景变成白色
sinplot()

png

1
2
sns.set_style('ticks')    # 横纵坐标轴加上刻度 
sinplot()

png

despine参数使用

用于隐藏某个刻度轴

1
2
3
4
5
6
7
8
9
10
11
12
13
Signature:
sns.despine(
fig=None,
ax=None,
top=True,
right=True,
left=False,
bottom=False,
offset=None,
trim=False,
)
Docstring:
Remove the top and right spines from plot(s).
1
2
sinplot()
sns.despine() # 只保留XY轴上的刻度

png

1
2
sns.violinplot(data)
sns.despine(offset=10) # 图形与刻度轴的距离

png

1
2
sns.violinplot(data)
sns.despine(offset=60)

png

1
2
3
sns.set_style("whitegrid")
sns.boxplot(data=data, palette="deep")
sns.despine(left=True) # 隐藏左边的轴

png

1
2
3
sns.set_style("whitegrid")
sns.boxplot(data=data, palette="deep")
sns.despine(bottom=True)

png

子图相关

1
2
3
4
5
6
7
8
# 每个子图的风格不同

with sns.axes_style("darkgrid"): # with语句中是指定的风格
plt.subplot(211)
sinplot() # 调用函数

plt.subplot(212) # 不在with里面是另一种风格
sinplot(-1)

png

布局设置

1
sns.set()
1
2
3
sns.set_context("paper")
plt.figure(figsize=(8,6))
sinplot()

png

1
2
3
sns.set_context("talk")   # 线更粗 刻度值更大了点
plt.figure(figsize=(8,6))
sinplot()

png

1
2
3
sns.set_context("poster")   # 线更粗 刻度值更大了点
plt.figure(figsize=(8,6))
sinplot()

png

1
2
3
4
5
sns.set_context("notebook", 
font_scale=1.5, # 刻度值大小
rc={"lines.linewidth":2.5} # 线条粗细
)
sinplot()

png

1
2
3
4
5
sns.set_context("notebook", 
font_scale=2.5, # 刻度值大小
rc={"lines.linewidth":4} # 线条粗细
)
sinplot()

png

本文标题:seaborn画风和主题风格

发布时间:2019年10月23日 - 11:10

原始链接:http://www.renpeter.cn/2019/10/23/seaborn%E7%94%BB%E9%A3%8E.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

Coffee or Tea