Seaborn线型图显示自定义中心线,而不是平均值

2024-06-26 14:56:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据集,每个时间步由三个值组成:一个是平均值,一个是误差下限和误差上限

name,year,area
test,2017,1.0376800009967053 #mean
test,2017,0.09936810445983806 #lower bound
test,2017,2.118230806622908 #upper bound and so on ...
test,2018,1.0
test,2018,0.13705391957353763
test,2018,2.1881023056535183
test,2019,1.2928531655977922
test,2019,0.17400072775054737
test,2019,3.016064939443665

我想绘制数据,以便在上限和下限之间得到一个阴影区域,并在数据集中的平均值后面有一条线

我试过seaborn.lineplot (https://seaborn.pydata.org/examples/errorband_lineplots.html) 但是,它会计算树值的平均值,因此该线不是实际平均值应位于的位置。 有人有什么想法吗? 有可能改变seaborn计算中心线的方式吗?(例如中位数)


Tags: and数据nametest时间areaseabornmean
1条回答
网友
1楼 · 发布于 2024-06-26 14:56:22

您可以使用seaborn.lineplotestimator关键字。在documentation中,您可以找到与此相关的:

estimator : name of pandas method or callable or None, optional

Method for aggregating across multiple observations of the y variable at the same x level. If None, all observations will be drawn.

estimator的默认值为mean,这解释了您在问题中描述的观察结果。因此,您可以定义一个lambda函数,始终选择相同year的三个值中的第一个值

lambda x: x[0]

使用

import seaborn as sns
sns.lineplot(x='year', y='area', data=df, estimator=lambda x: x[0], marker='o')

给你想要的情节

enter image description here

如果您想改为使用median,请在import numpy as np之前使用estimator=np.median

相关问题 更多 >