为什么seaborn图不显示置信区间/误差线?

问题描述 投票:0回答:1

我使用

sns.lineplot
在图中显示置信区间。

sns.lineplot(x = threshold, y = mrl_array, err_style = 'band', ci=95)
plt.show()

我得到以下图,它没有显示置信区间:

有什么问题吗?

python python-3.x seaborn confidence-interval errorbar
1个回答
2
投票

errorbar
string
,(字符串,数字)元组,可调用或
None
errorbar 方法的名称(
'ci'
'pi'
'se'
'sd'
),或具有方法名称和级别参数的元组,或从向量映射到 (min, max ) 间隔,或
None
隐藏错误栏。

默认为

errorbar=('ci', 95)


  • 每个 x 值可能只有一个观察值。
    • 如果每个 x 值只有一个观测值,则没有可绘制的置信区间。
  • 引导是根据 x 值执行的,但需要多个观察才能使其生效。

    ci
    :使用估计器聚合时要绘制的置信区间的大小。
    'sd'
    表示绘制数据的标准差。设置为
    None
    将跳过 引导。

  • 请注意以下来自
    seaborn.lineplot
    的示例。
  • 对于
    sns.relplot
    kind='line'
    也是如此。
  • 问题指定了
    sns.lineplot
    ,但此答案适用于任何显示置信区间seaborn图,例如
    seaborn.barplot

数据

import seaborn as sns

# load data
flights = sns.load_dataset("flights")

   year month  passengers
0  1949   Jan         112
1  1949   Feb         118
2  1949   Mar         132
3  1949   Apr         129
4  1949   May         121
# only May flights
may_flights = flights.query("month == 'May'")

     year month  passengers
4    1949   May         121
16   1950   May         125
28   1951   May         172
40   1952   May         183
52   1953   May         229
64   1954   May         234
76   1955   May         270
88   1956   May         318
100  1957   May         355
112  1958   May         363
124  1959   May         420
136  1960   May         472

# standard deviation for each year of May data
may_flights.set_index('year')[['passengers']].std(axis=1)

year
1949   NaN
1950   NaN
1951   NaN
1952   NaN
1953   NaN
1954   NaN
1955   NaN
1956   NaN
1957   NaN
1958   NaN
1959   NaN
1960   NaN
dtype: float64
# flight in wide format
flights_wide = flights.pivot("year", "month", "passengers")

month  Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
year                                                             
1949   112  118  132  129  121  135  148  148  136  119  104  118
1950   115  126  141  135  125  149  170  170  158  133  114  140
1951   145  150  178  163  172  178  199  199  184  162  146  166
1952   171  180  193  181  183  218  230  242  209  191  172  194
1953   196  196  236  235  229  243  264  272  237  211  180  201
1954   204  188  235  227  234  264  302  293  259  229  203  229
1955   242  233  267  269  270  315  364  347  312  274  237  278
1956   284  277  317  313  318  374  413  405  355  306  271  306
1957   315  301  356  348  355  422  465  467  404  347  305  336
1958   340  318  362  348  363  435  491  505  404  359  310  337
1959   360  342  406  396  420  472  548  559  463  407  362  405
1960   417  391  419  461  472  535  622  606  508  461  390  432

# standard deviation for each year
flights_wide.std(axis=1)

year
1949    13.720147
1950    19.070841
1951    18.438267
1952    22.966379
1953    28.466887
1954    34.924486
1955    42.140458
1956    47.861780
1957    57.890898
1958    64.530472
1959    69.830097
1960    77.737125
dtype: float64

情节

  • may_flights
    每年有一次观察,因此没有显示 CI。
sns.lineplot(data=may_flights, x="year", y="passengers")

sns.barplot(data=may_flights, x='year', y='passengers')

  • flights_wide
    显示每年有 12 个观测值,因此 CI 显示绘制所有
    flights
    时的情况。
sns.lineplot(data=flights, x="year", y="passengers")

sns.barplot(data=flights, x='year', y='passengers')

© www.soinside.com 2019 - 2024. All rights reserved.