马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
目录
案例
学习网址:https://seaborn.pydata.org/examples/errorband_lineplots.html- import seaborn as sns
- import pandas as pd
- sns.set_theme()
- # 导入数据
- fmri = pd.read_csv("../../seaborn-data-master/fmri.csv")
- # 查看数据
- fmri.head()
复制代码 subjecttimepointeventregionsignal0s1318stimparietal-0.0175521s514stimparietal-0.0808832s1218stimparietal-0.0810333s1118stimparietal-0.0461344s1018stimparietal-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()
复制代码 yearmonthpassengers01949January11949February21949March31949April41949May- 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()
复制代码 monthAprilAugustDecemberFebruaryJanuaryJulyJuneMarchMayNovemberOctoberSeptemberyear19491291481181181121481351321211041191361950135170140126115170149141125114133158195116319916615014519917817817214616218419521812421941801712302181931831721912091953235272201196196264243236229180211237- 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__
复制代码 example 8
- ## 加载数据
- fmri = pd.read_csv("../../seaborn-data-master/fmri.csv")
- fmri.head()
复制代码 subjecttimepointeventregionsignal0s1318stimparietal1s514stimparietal2s1218stimparietal3s1118stimparietal4s1018stimparietal- 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()
复制代码 alignchoicetimecoherencefiring_rate0dotsT1-800.01dotsT1-803.22dotsT1-806.43dotsT1-8012.84dotsT1-8025.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'
- )
复制代码
|