使用中位数而不是均值的 Seaborn 线图

Seaborn lineplot using median instead of mean

我正在使用 seaborn.lineplot() 创建一个像这样的线图(一条线代表平均值,被代表标准差的带包围):

sns.lineplot(x="trial", y="rvalues", hue="subject", err_style="band", ci='sd', data=df)

我唯一的问题是因为我的数据不是高斯数据,所以我更关心 中位数 而不是 均值 。如何在 Seaborn 中做到这一点?

或者有没有类似的工具可以使用?我知道我可以在 matplotlib 中从头开始做,但这需要大量的工作才能让它变得这么好。

来自the 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.

因此尝试

sns.lineplot(x="trial", y="rvalues", hue="subject", err_style="band", 
             ci='sd', estimator="median", data=df)

这是一个最小的例子:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

df = pd.DataFrame({"trial" : np.random.randint(10, size=350),
                   "rvalues" : np.random.randn(350),
                   "subject" : np.random.randint(4, size=350)})

sns.lineplot(x="trial", y="rvalues", hue="subject", err_style="band", 
             ci='sd', estimator="median", data=df)
plt.show()

estimator 应该是 pandas 方法。

使用 estimator=np.median 而不是 estimator="median"

sns.lineplot(x="trial", y="rvalues", hue="subject", err_style="band",
             ci='sd',
             estimator=np.median,
             data=df)