Python 用 seaborn 绘制误差线误差线位置错误

问题描述

我正在尝试使用 Seaborn 绘制一些具有置信区间的数据点。我的代码如下。我遇到的主要问题是置信区间没有映射到正确的数据点上,我不确定为什么?为我在此处创建数据框的轻微迂回方式道歉。

#Create dataframe
df = pd.DataFrame(np.array([['a','a','b','c','d','e','f','g','g'],['0','1','0','1'],[1.0,0.68,1.01,0.0,1.04,0.73,0.76,0.81,0.86,1.36,1.12,0.96],[0.95,0.53,0.93,0.98,0.4,0.63,0.59,0.48,1.03,0.82],[1.05,1.1,1.09,1.13,0.9,1.0,1.24,1.73,1.21,1.11]]))
#Transpose dataframe,names columns,and convert Z,CI_low,and CI_high columnns to floats
df = df.T
df.columns = ['group','Z','CI_low','CI_high']
df['Z'] = [float(x) for x in df.Z]
df['CI_low'] = [float(x) for x in df.CI_low]
df['CI_high'] = [float(x) for x in df.CI_high]

#Create error bar data
error=[df["Z"]-df['CI_low'],df['CI_high']-df["Z"]]

#Plot using group as x data,and Z as y data
ax = sns.pointplot('group',hue='c',data=df,dodge=True,join=False,ci=None,legend=False)

#Loop through datapoints setting coordinates
x_coords = []
y_coords = []
for point_pair in ax.collections:
    for x,y in point_pair.get_offsets():
        x_coords.append(x)
        y_coords.append(y)

#Plot error bar
ax.errorbar(x_coords,y_coords,yerr=error,fmt=' ',zorder=-1,solid_capstyle='projecting',capsize=6,ecolor='k')

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)