seaborn学习笔记

seaborn简单使用

导入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import seaborn as sns

# 默认风格
sns.set()
# 五种风格 darkgrid whitegrid dark white ticks
sns.set_style('darkgrid')
# offset 到轴线的距离 left 去掉左边的轴 right top bottom
sns.despine(offset = 10,left = True) # 去掉某些轴
# paper talk poster notebook 绘制内容风格
sns.set_context("paper",font_scale,rc={"lines.linewidth":2.5})

# bins 直方图的个数
sns.distplot(data,bins,kds=False) #画图

sns.jointplot(x,y,data) # 绘制散点图和单变量直方图

颜色

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 调色板 deep muted pastel bright dark colorblind
color_palette()
set_palette()

# 调色板输出8种颜色 均分
sns.palplot(sns.color_palette('hls',8))

# 调整亮度和饱和度 l=lightness s饱和度 saturation
sns.palplot(sns.hls_palette(8,l=3,s=9))

# 深浅
sns.palplot(sns.color_paltte("Paired"),8)
# xkcd 指定固定颜色
# sns.xkcd_rgb['pale red']

# 连续颜色 产生由浅到深的颜色 渐变翻转加_r 如BuGn_r n指定产生几种颜色 ‘cubehelix’线性变换
sns.palplot(sns.color_palette("Blues",n))

# n产生几种颜色 start开始颜色 rot变换度
sns.paplot(sns.cubehelix_palette(n,start,rot))

# 指定颜色渐变 由浅到深 dark_palette() 由深到浅 reverse 翻转
sns.palplot(sns.light_palette('green',reverse))

使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt


def sinplot(flip=1):
# 0到14之间取100个点
x = np.linspace(0, 14, 100)
style = ['darkgrid', 'whitegrid', 'dark', 'white', 'ticks']
for j in range(5):
plt.subplot(3, 2, j+1)
for i in range(1, 7):
plt.plot(x, np.sin(x+i*.5)*(7-i)*flip)
sns.set_style(style[j])
plt.title = style[j]



sinplot()
plt.show()

"jointplot"

散点图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

mean, cov = [0, 1], [(1, 0.5), (.5, 1)]
# 生成200个数据 两组
data = np.random.multivariate_normal(mean, cov, 200)

data = pd.DataFrame(data, columns=["x", "y"])
print(data)

sns.jointplot(x="x", y="y", data=data)
# kind 指定点的样式 hex六边形
# sns.jointplot(x="x", y="y",kind="hex", data=data)
plt.show()

"jointplot"

回归

1
2
3
4
5
6
7
# 回归分析
regplot()
lmplot()


# regplot画回归 x_jitter抖动
sns.regplot(x,y,data,x_jitter)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd


# 自带数据集
# iris = sns.load_dataset("tips")
# iris.head()
mean, cov = [0, 1], [(1, 0.5), (.5, 1)]
# 生成200个数据 两组
data = np.random.multivariate_normal(mean, cov, 200)

data = pd.DataFrame(data, columns=["x", "y"])
print(data)

sns.regplot(x="x", y="y", data = data)
plt.show()

"jointplot"