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

[复制链接]
发表于 2022-9-21 20:39:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
目录

案例

学习网址:https://seaborn.pydata.org/examples/errorband_lineplots.html
  1. import seaborn as sns
  2. import pandas as pd
  3. sns.set_theme()
  4. # 导入数据
  5. fmri = pd.read_csv("../../seaborn-data-master/fmri.csv")
  6. # 查看数据
  7. fmri.head()
复制代码
subjecttimepointeventregionsignal0s1318stimparietal-0.0175521s514stimparietal-0.0808832s1218stimparietal-0.0810333s1118stimparietal-0.0461344s1018stimparietal-0.037970
  1. # 画有误差错误带的时序图
  2. sns.lineplot(
  3.     x = "timepoint", y = 'signal',
  4.     hue = 'region', style = 'event',
  5.     data = fmri
  6. )
复制代码

sns.lineplot() 的案例

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

example 2

换一种形式处理数据
  1. flights_wide = flights.pivot("year", "month", "passengers")
  2. '''
  3.     参数解读:
  4.         year : 指定每一行的输出内容
  5.         month : 指定每一列的输出内容
  6.         passengers : 指定输出的内容
  7. '''
  8. flights_wide.head()
复制代码
monthAprilAugustDecemberFebruaryJanuaryJulyJuneMarchMayNovemberOctoberSeptemberyear19491291481181181121481351321211041191361950135170140126115170149141125114133158195116319916615014519917817817214616218419521812421941801712302181931831721912091953235272201196196264243236229180211237
  1. sns.lineplot(data = flights_wide['May'])
复制代码

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

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

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

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

可以发现颜色图例和example3一致(除了月份顺序)
example 7
  1. sns.lineplot(data = flights, x = 'passengers', y = 'year', orient = 'y')
  2. # xy轴互换,此时 1个 y 对应多个 x ,如果直接画的画有点类似于 迪利克雷函数 那种 图像形式,
  3. # 而加上 orient = 'y' 就能得到每一年的平均值,并画出95%的置信区间
复制代码

注:使用 orient 参数前需要将seaborn版本升级到 0.12.0
  1. # 查看 seaborn 版本
  2. sns.__version__
复制代码
  1. '0.12.0'
复制代码
example 8
  1. ## 加载数据
  2. fmri = pd.read_csv("../../seaborn-data-master/fmri.csv")
  3. fmri.head()
复制代码
subjecttimepointeventregionsignal0s1318stimparietal1s514stimparietal2s1218stimparietal3s1118stimparietal4s1018stimparietal
  1. sns.lineplot(data = fmri, x = 'timepoint', y = 'signal', hue = 'event') # 以 event 为依据分类
复制代码

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

example 10
  1. # 可以用 markers = True 进行描点
  2. sns.lineplot(
  3.     data=fmri,
  4.     x='timepoint',
  5.     y='signal',
  6.     hue='event',
  7.     style='event',
  8.     markers=True, # 不同类别线条上的描点进行区别
  9.     dashes=False # 不同类别线条的形状不进行区别
  10. )
复制代码

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

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

example 13
  1. # 加载新的数据集
  2. dots = pd.read_csv("../../seaborn-data-master/dots.csv").query("align == 'dots'")
  3. dots.head()
复制代码
alignchoicetimecoherencefiring_rate0dotsT1-800.01dotsT1-803.22dotsT1-806.43dotsT1-8012.84dotsT1-8025.6
  1. # 对不同的coherence(数字变量)调不同的颜色,以 choice 为依据划分线条的类型
  2. import matplotlib.pyplot as plt
  3. plt.figure(figsize = (8,6))
  4. sns.lineplot(
  5.     data = dots,
  6.     x = 'time',
  7.     y = 'firing_rate',
  8.     hue = 'coherence',
  9.     style = 'choice'
  10. )
  11. plt.legend(loc='upper left')
复制代码

example 14
  1. # 可以以python列表或字典的形式传递特定的颜色
  2. import matplotlib.pyplot as plt
  3. plt.figure(figsize = (8,6))
  4. palette = sns.color_palette('mako_r', 6)
  5. sns.lineplot(
  6.     data = dots,
  7.     x = 'time',
  8.     y = 'firing_rate',
  9.     hue = 'coherence',
  10.     style = 'choice',
  11.     palette = palette
  12. )
  13. plt.legend(loc='upper left')
复制代码

example 15
  1. # 不同 coherence 类别可以用不同粗细的线条画出
  2. plt.figure(figsize=(8,6))
  3. sns.lineplot(
  4.     data = dots,
  5.     x = 'time',
  6.     y = 'firing_rate',
  7.     size = 'coherence',
  8.     hue = 'choice',
  9.     legend = 'full'
  10. )
  11. plt.legend(loc='upper left')
复制代码
  1. # 可以手动调节线条粗细的范围区间
  2. plt.figure(figsize=(8,6))
  3. sns.lineplot(
  4.     data = dots,
  5.     x = 'time',
  6.     y = 'firing_rate',
  7.     size = 'coherence',
  8.     hue = 'choice',
  9.     sizes = (.25, 2.5) #定义最小值和最大值
  10. )
  11. plt.legend(loc='upper left')
复制代码

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

example 17
  1. # 使用 replot() 分组绘图,以参数 col 进行分组
  2. sns.relplot(
  3.     data = fmri,
  4.     x = 'timepoint',
  5.     y = 'signal',
  6.     col = 'region',
  7.     hue = 'event',
  8.     style = 'event',
  9.     kind = 'line'
  10. )
复制代码

回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表