【问题标题】:Adding error bars to seaborn scatter plot (when a line plot is combined)向 seaborn 散点图添加误差线(当组合线图时)
【发布时间】:2021-08-06 18:50:14
【问题描述】:

我在 seaborn 中有一个散点图 + 线图,以这种方式创建:

import seaborn as sns
import pandas as pd

# load sample data from seaborn
flights = sns.load_dataset('flights')

fig_example = plt.figure(figsize=(10, 10))
sns.lineplot(data=flights, x="year", y="passengers", hue="month")
sns.scatterplot(data=flights, x="year", y="passengers", hue="month",legend=False)

现在,我想添加误差线。例如,第一个入口点是(年=1949,乘客=112)。我想为这个特定项目添加一个标准。例如:+= 5 名乘客。我该怎么做?

这个问题没有回答我的问题:How to use custom error bar in seaborn lineplot?

我需要将它添加到散点图中。不是线图。

当我尝试这个命令时:

ax = sns.scatterplot(x="x", y="y", hue="h", data=gqa_tips, s=100, ci='sd', err_style='bars')

失败了:

AttributeError: 'PathCollection' object has no property 'err_style'

谢谢。

【问题讨论】:

    标签: matplotlib seaborn data-visualization scatter-plot errorbar


    【解决方案1】:
    • 这个问题似乎显示了对误差线/置信区间的误解 (ci)。
      • 具体来说,...第一个入口点...我想为这个特定项目添加一个 std
    • 在单个数据点上放置误差条是不正确的统计表示,因为这些单个数据点没有错误,至少在与问题相关时没有错误。
    • 图中的每个点都没有错误,因为它是一个精确值。
      • 相对于所有真实数据点,聚合值(例如平均值)具有ci。
    • 在没有hue 的lineplot 中生成的聚合值将使用estimator='mean',然后将使用ci。
    • 请参考How to use custom error bar in seaborn lineplot 自定义ci。
    import pandas as pd
    import seaborn as sns
    
    # load the data
    flights = sns.load_dataset('flights')
    
    # plots
    fig, (ax1, ax2, ax3) = plt.subplots(ncols=3, figsize=(18, 7))
    sns.lineplot(data=flights, x="year", y="passengers", marker='o', ci=95, ax=ax1, label='Mean CI: 95')
    ax1.set(title='Mean Passengers per Year')
    
    sns.lineplot(data=flights, x="year", y="passengers", ci='sd', err_style='bars', ax=ax2, label='Mean CI: sd')
    flights.groupby('year').passengers.agg([min, max]).plot(ax=ax2)
    ax2.set(title='Mean Min & Max Passengers per Year')
    
    sns.lineplot(data=flights, x="year", y="passengers", hue="month", marker='o', ax=ax3)
    ax3.set(title='Individual Passengers per Month\nNo CI for Individual Points')
    

    【讨论】:

      猜你喜欢
      • 2020-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-20
      • 1970-01-01
      • 2015-06-29
      • 1970-01-01
      • 2019-08-17
      相关资源
      最近更新 更多