【发布时间】:2020-12-31 18:46:52
【问题描述】:
我想将我计算的 HDI(高密度区间)(下面 df 中的列 hdi_both、hdi_one 和 lower_upper)添加到条形图中。
但是,我无法弄清楚如何添加误差线/CI,以便每个误差线都有一个自定义的上下界,它们独立于 y 值(在本例中为 proportion_correct 中的对应值)。
例如,Exp.的 HDI 间隔。 1 与guesses_correct both 的下限为0.000000 ,上限为0.130435,proportion_correct 为 0.000000。
我看到的所有选项都包括指定相对于 y 轴上的值的上限和下限,这不是我想要的。
任何帮助将不胜感激。
谢谢,
阿亚拉
import os
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.DataFrame({
'exp': ['Exp. 1', 'Exp. 1', 'Exp. 2', 'Exp. 2', 'Exp. 3', 'Exp. 3', 'Exp. 4', 'Exp. 4', 'Exp. 5', 'Exp. 5',
'Collapsed', 'Collapsed'],
'proportion_correct': [0.0, 0.304347826, 0.058823529000000006, 0.31372549, 0.047619048, 0.333333333, 0.12244898, 0.428571429, 0.12244898, 0.367346939, 0.082901554, 0.35751295299999997],
'guesses_correct': ['both', 'one', 'both', 'one', 'both', 'one', 'both', 'one', 'both', 'one', 'both', 'one'],
'hdi_both': [0.0, 0.130434783, 0.0, 0.078431373, 0.0, 0.1, 0.0, 0.08, 0.0, 0.081632653, 0.005181347, 0.051813472],
'hdi_one': [0.130434783, 0.47826087, 0.156862745, 0.41176470600000004, 0.1, 0.5, 0.16, 0.4, 0.163265306, 0.408163265, 0.21761658, 0.341968912],
'lower_upper': ['lower', 'upper', 'lower', 'upper', 'lower', 'upper', 'lower', 'upper', 'lower', 'upper', 'lower', 'upper']
})
print(df.head())
Out[4]:
exp proportion_correct guesses_correct hdi_both hdi_one lower_upper
0 Exp. 1 0.000000 both 0.000000 0.130435 lower
1 Exp. 1 0.304348 one 0.130435 0.478261 upper
2 Exp. 2 0.058824 both 0.000000 0.156863 lower
3 Exp. 2 0.313725 one 0.078431 0.411765 upper
4 Exp. 3 0.047619 both 0.000000 0.100000 lower
# Make bar plot
sns.barplot(x='exp',
y='proportion_correct',
hue='guesses_correct',
data=df)
plt.ylim([0, 0.5])
plt.xlabel('Experiment')
plt.ylabel('Proportion Correct')
plt.legend(title='Correct guesses', loc='upper right')
plt.axhline(y=0.277777, color='dimgray', linestyle='--')
plt.annotate(' chance\n one', (5.5, 0.27))
plt.axhline(y=0.02777, color='dimgray', linestyle='--')
plt.annotate(' chance\n both', (5.5, 0.02))
# Show the plot
plt.show()
【问题讨论】:
标签: python pandas matplotlib seaborn errorbar