Seaborn第三章:带有误差范围的时间序列图

案例

学习网址:https://seaborn.pydata.org/examples/errorband_lineplots.html

import seaborn as sns
import pandas as pd
sns.set_theme(style="darkgrid")
# 导入数据
fmri = pd.read_csv("../../seaborn-data-master/fmri.csv")
# 查看数据
fmri.head()
subject timepoint event region signal
0 s13 18 stim parietal -0.017552
1 s5 14 stim parietal -0.080883
2 s12 18 stim parietal -0.081033
3 s11 18 stim parietal -0.046134
4 s10 18 stim parietal -0.037970
# 画有误差错误带的时序图
sns.lineplot(
x = "timepoint", y = 'signal',
hue = 'region', style = 'event',
data = fmri
)

sns.lineplot() 的案例

example 1

# 导入数据
import pandas as pd
import seaborn as sns
flights = pd.read_csv("../../seaborn-data-master/flights.csv") # 10年中
flights.head()
year month passengers
0 1949 January
1 1949 February
2 1949 March
3 1949 April
4 1949 May
may_flights = flights.query("month == 'May'")
# may_flights = flights.loc[flights["month"] == 'May'] 也行
print(may_flights)
sns.lineplot(data = may_flights, x = 'year', y = 'passengers')
[out]:
year month passengers
4 1949 May 121
16 1950 May 125
28 1951 May 172
40 1952 May 183
52 1953 May 229
64 1954 May 234
76 1955 May 270
88 1956 May 318
100 1957 May 355
112 1958 May 363
124 1959 May 420
136 1960 May 472

example 2

换一种形式处理数据

flights_wide = flights.pivot("year", "month", "passengers")
'''
参数解读:
year : 指定每一行的输出内容
month : 指定每一列的输出内容
passengers : 指定输出的内容
'''
flights_wide.head()
month April August December February January July June March May November October September
year
1949 129 148 118 118 112 148 135 132 121 104 119 136
1950 135 170 140 126 115 170 149 141 125 114 133 158
1951 163 199 166 150 145 199 178 178 172 146 162 184
1952 181 242 194 180 171 230 218 193 183 172 191 209
1953 235 272 201 196 196 264 243 236 229 180 211 237
sns.lineplot(data = flights_wide['May'])

example 3

## 可以输出多列的线型图
import matplotlib.pyplot as plt
plt.figure(figsize=(8, 6)) # 为了让图例不覆盖曲线
sns.lineplot(data = flights_wide)
plt.legend(loc='upper left') # 设置图例的位置

example 4

# 绘制每年的平均值以及95%的置信区间
sns.lineplot(data = flights, x = 'year', y = 'passengers')

example 5

# 可以对不同分组应用不同的颜色:hue
sns.lineplot(data = flights, x = 'year', y = 'passengers', hue = 'month') # 用不同的颜色区分不同的月份

example 6

# 可以通过调节 style 参数更改线条的类型
plt.figure(figsize=(8,6))
sns.lineplot(data = flights, x = 'year', y = 'passengers', hue = 'month', style = 'month')
plt.legend(loc='upper left')

可以发现颜色图例和example3一致(除了月份顺序)

example 7

sns.lineplot(data = flights, x = 'passengers', y = 'year', orient = 'y')
# xy轴互换,此时 1个 y 对应多个 x ,如果直接画的画有点类似于 迪利克雷函数 那种 图像形式,
# 而加上 orient = 'y' 就能得到每一年的平均值,并画出95%的置信区间

注:使用 orient 参数前需要将seaborn版本升级到 0.12.0

# 查看 seaborn 版本
sns.__version__
'0.12.0'

example 8

## 加载数据
fmri = pd.read_csv("../../seaborn-data-master/fmri.csv")
fmri.head()
subject timepoint event region signal
0 s13 18 stim parietal
1 s5 14 stim parietal
2 s12 18 stim parietal
3 s11 18 stim parietal
4 s10 18 stim parietal
sns.lineplot(data = fmri, x = 'timepoint', y = 'signal', hue = 'event') # 以 event 为依据分类

example 9

# 接着 example 8 的例子,用不同的色调区分 region,用不同的线条类型区分 event
sns.lineplot(data=fmri, x='timepoint', y='signal', hue='region', style='event')

example 10

# 可以用 markers = True 进行描点
sns.lineplot(
data=fmri,
x='timepoint',
y='signal',
hue='event',
style='event',
markers=True, # 不同类别线条上的描点进行区别
dashes=False # 不同类别线条的形状不进行区别
)

example 11

# 这次利用误差线而不是误差宽度
sns.lineplot(
data = fmri,
x='timepoint',
y='signal',
hue='event',
err_style='bars',
errorbar=('se',2) # se代表样本标准误差
)
# errorbar可选参数有 'ci' 'se' 'sd' 'pi'
# ci 是显著性检验的补充,反映的是真实的均值或者均值区别的范围,即置信区间
# se 标准误差(Standard Error)
# sd 样本标准差

example 12

# 使用 units 参数进行分组
sns.lineplot(
data = fmri.query("region == 'frontal'"),
x='timepoint',
y='signal',
hue='event',
units='subject',
estimator=None, #用于在同一水平x上聚合y变量的多个观测值的方法,如果是 None,将绘制所有观测值
# lw=1
)

example 13

# 加载新的数据集
dots = pd.read_csv("../../seaborn-data-master/dots.csv").query("align == 'dots'")
dots.head()
align choice time coherence firing_rate
0 dots T1 -80 0.0
1 dots T1 -80 3.2
2 dots T1 -80 6.4
3 dots T1 -80 12.8
4 dots T1 -80 25.6
# 对不同的coherence(数字变量)调不同的颜色,以 choice 为依据划分线条的类型
import matplotlib.pyplot as plt
plt.figure(figsize = (8,6))
sns.lineplot(
data = dots,
x = 'time',
y = 'firing_rate',
hue = 'coherence',
style = 'choice'
)
plt.legend(loc='upper left')

example 14

# 可以以python列表或字典的形式传递特定的颜色
import matplotlib.pyplot as plt
plt.figure(figsize = (8,6))
palette = sns.color_palette('mako_r', 6)
sns.lineplot(
data = dots,
x = 'time',
y = 'firing_rate',
hue = 'coherence',
style = 'choice',
palette = palette
)
plt.legend(loc='upper left')

example 15

# 不同 coherence 类别可以用不同粗细的线条画出
plt.figure(figsize=(8,6))
sns.lineplot(
data = dots,
x = 'time',
y = 'firing_rate',
size = 'coherence',
hue = 'choice',
legend = 'full'
)
plt.legend(loc='upper left')

# 可以手动调节线条粗细的范围区间
plt.figure(figsize=(8,6))
sns.lineplot(
data = dots,
x = 'time',
y = 'firing_rate',
size = 'coherence',
hue = 'choice',
sizes = (.25, 2.5) #定义最小值和最大值
)
plt.legend(loc='upper left')

example 16

# 默认情况下,绘图时按 x 排序,若 调节 sort = False,则绘图时按数据集中的顺序绘制
import numpy as np
x, y = np.random.normal(size = (2, 5000)).cumsum(axis = 1)
sns.lineplot(x=x, y=y, lw=1)

sns.lineplot(x=x, y=y, sort=False, lw=1)

example 17

# 使用 replot() 分组绘图,以参数 col 进行分组
sns.relplot(
data = fmri,
x = 'timepoint',
y = 'signal',
col = 'region',
hue = 'event',
style = 'event',
kind = 'line'
)

posted @   只会加减乘除  阅读(1024)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
点击右上角即可分享
微信分享提示