如何为Seaborn条形图指定不确定度条形?

时间:2019-05-09 15:46:10

标签: matplotlib bar-chart seaborn

可以通过以下方式创建Seaborn barplot:

import pandas as pd
import seaborn as sns

df = pd.DataFrame(
         [
             ['variable_a', 0.656536, 0.054560],
             ['variable_b', 0.425124, 0.056104],
             ['variable_c', 0.391201, 0.049393],
             ['variable_d', 0.331990, 0.032777],
             ['variable_e', 0.309588, 0.027449],
         ],
         columns = [
             'index',
             'mean',
             'statistical_uncertainty'
         ]
    )
df.index = df['index']
del df['index']
df

p = sns.barplot(df["mean"], df.index);
plt.show;

如何将不确定性条形图添加到条形图中?这似乎是一种有前途的方法,但是我不确定如何进行:https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.errorbar.html

1 个答案:

答案 0 :(得分:2)

您可以使用图解上方的plt.errorbar绘制误差线:

p = sns.barplot(df["mean"], df.index)

# to enhance visibility of error bars, 
# you can draw them twice with different widths and colors:
p.errorbar(y=range(len(df)), 
           x=df['mean'], 
           xerr=df.statistical_uncertainty, 
           fmt='none',
           linewidth=3, c='w')

p.errorbar(y=range(len(df)), 
           x=df['mean'], 
           xerr=df.statistical_uncertainty, 
           fmt='none',
           c='r')
plt.show;

enter image description here