这个透明的扩展是如何在 lineplot 中附带一个 plot 的?

How does this transparent extension come with a plot in lineplot?

文档中的情节如下所示: 代码

sns.lineplot(x="timepoint", y="signal",
             hue="region", style="event",
             data=fmri)

和 我的结果是这样的 对于代码:

sns.lineplot(
            # data=fmri, 
             x=df["_C_UP"]["s"][:10],
             y=df["_C_UP"]["px"][:10]
            #  hue="event"
             );

如何为这些线条(周围的透明颜色)获得相同的效果

这是我的数据的样子

#Energy s   py  pz  px  dxy dyz dz2 dxz dx2 tot
50  -17.98094   0.72320 0.31781 0.00000 0.31882 0.0 0.0 0.0 0.0 0.0 1.35982
51  -17.87394   0.29726 0.14415 0.00000 0.14491 0.0 0.0 0.0 0.0 0.0 0.58632
52  -17.76794   0.63694 0.02456 0.00000 0.02484 0.0 0.0 0.0 0.0 0.0 0.68634
53  -17.66194   1.78595 0.06032 0.00001 0.06139 0.0 0.0 0.0 0.0 0.0 1.90766
54  -17.55494   1.97809 0.09038 0.00001 0.09192 0.0 0.0 0.0 0.0 0.0 2.16040

这取决于数据。您显示的 seaborn 文档中的图基于一个数据集,其中每个 x 值都有多个 y 值(重复测量)。图中的线条表示这些 y 值的平均值,阴影区域表示相关的 95% 置信区间。

在您的数据中,每个 x 值只有一个 y 值,因此无法计算置信区间。

fmri数据集中,每个时间点和子组实际上有多个观察值,例如,在时间点== 14:

fmri[fmri['timepoint']==14]

    subject timepoint   event   region  signal
1   s5  14  stim    parietal    -0.080883
57  s13 14  stim    parietal    -0.033713
58  s12 14  stim    parietal    -0.068297
59  s11 14  stim    parietal    -0.114469
60  s10 14  stim    parietal    -0.052288
61  s9  14  stim    parietal    -0.130267

因此,您看到的线实际上是所有这些观察结果的平均值(按组分层),而色带是该平均值的 95% 置信区间。例如,您可以通过以下方式关闭此功能:

sns.lineplot(x="timepoint", y="signal",
             hue="region", style="event",
             data=fmri,ci=None)

因此,要获得准确的图,您需要进行多次观察或重复。如果你不这样做,而你的意图只是连接点,你就无法获得置信区间。

如果您想查看趋势线,可以尝试的一种方法是多项式平滑。将数据绘制为点也很有意义。

使用来自同一 fmri 数据集的示例:

df = fmri[(fmri['subject']=="s5") & (fmri['event']== "stim") & (fmri['region'] == "frontal")]
sns.regplot(data=df,x = "timepoint",y = "signal",order=3)

或者用黄土抹平,比较复杂(下面画的是什么this post

import matplotlib.pyplot as plt
from skmisc.loess import loess

lfit = loess(df['timepoint'],df['signal'])
lfit.fit()
pred = lfit.predict(df['timepoint'], stderror=True)
conf = pred.confidence()

fig, ax = plt.subplots()
sns.scatterplot(data=df,x = "timepoint",y = "signal",ax=ax)
sns.lineplot(x = df["timepoint"],y = pred.values,ax=ax,color="#A2D2FF")
ax.fill_between(df['timepoint'],conf.lower, conf.upper, alpha=0.1,color="#A2D2FF")