【问题标题】:Plot standard deviation from external datasource using seaborn使用 seaborn 绘制与外部数据源的标准差
【发布时间】:2026-01-30 05:35:01
【问题描述】:

我正在尝试通过 seaborn 可视化线图,我想在其中绘制列的平均值和标准差。由于我使用的是大文件(数百万行),因此该图需要一段时间才能加载。

为了减少计算时间,我预先计算了列的平均值和相应的标准差。随后,我使用这个预先计算的数据作为线图的输入,而不是提供完整的 Pandas 数据框。

这是我目前使用的代码:

df = open_pickle("data/experiment")
sns.lineplot(x="rho", y="wait_time_mean", hue="c", style="service_type", data=df)

这只会显示平均值。我想知道是否可以手动为 seaborn 提供标准偏差值。

【问题讨论】:

    标签: python pandas seaborn std


    【解决方案1】:

    sns.lineplot 返回绘图的 Axes 对象,然后可以使用该对象在其上绘图。假设您的标准偏差也在df 中,您可以通过以下方式调整您的代码,现在使用matplotlib 函数fill_beetween

    df = open_pickle("data/experiment")
    ax = sns.lineplot(x="rho", y="wait_time_mean", hue="c", style="service_type", data=df)
    ax.fill_between(df["rho"], y1=df["wait_time_mean"] - df["wait_time_std"], y2=df["wait_time_mean"] + df["wait_time_std"], alpha=.5)
    

    【讨论】: