如何控制 Python 中误差条图之间的一致性?

How can I control coherence between error bar plots in Python?

我有这个数据:

import pandas as pd
URL = "https://stepik.org/media/attachments/lesson/9250/atherosclerosis.csv"
data = pd.read_csv(URL)
atherosclerosis = data.groupby(["age", "dose"]).agg(['mean', 'std'])
atherosclerosis.columns = ['_'.join(col) for col in atherosclerosis.columns]
atherosclerosis

结果:

          expr_mean  expr_std
age dose                      
1   D1    104.758464  5.863454
    D2    105.545864  4.369024
2   D1    101.004805  5.116310
    D2    102.273629  5.135374

我这样绘制误差线:

plot_data1 = atherosclerosis.xs('D1', level=1, drop_level=False)
plot_data2 = atherosclerosis.xs('D2', level=1, drop_level=False)
plot_index1 = [str(idx) for idx in plot_data1.index]
plot_index2 = [str(idx) for idx in plot_data2.index]
plt.errorbar(plot_index1, plot_data1["expr_mean"], 
             yerr=plot_data1["expr_std"]/2,
             marker="s", mfc='green', 
             markeredgewidth=2, capsize=4, capthick=2, 
             fmt='o-', ecolor="magenta")
plt.errorbar(plot_index2, plot_data2["expr_mean"], 
             yerr=plot_data2["expr_std"]/2,
             marker="s", mfc='green', 
             markeredgewidth=2, capsize=4, capthick=2, 
             fmt='o-', ecolor="magenta")
plt.show()

结果:

]1

我能以某种方式将 (1, 'D1') 与 (1, 'D2') 以及 (2, 'D1') 与 (2, 'D2') 连接起来吗?像这样:

]2

您可以只更改绘图数据:

plot_data1 = atherosclerosis.xs(1, level=0, drop_level=False)
plot_data2 = atherosclerosis.xs(2, level=0, drop_level=False)

输出:


更新: 为了得到你想要的,我会对数据进行排序,并根据范围绘制它:

动脉粥样硬化 = atherosclerosis.sort_index(水平=(1,0)) 动脉粥样硬化['range'] = np.arange(len(动脉粥样硬化))

plot_data1 = atherosclerosis.xs(1, level=0, drop_level=False)
plot_data2 = atherosclerosis.xs(2, level=0, drop_level=False)
plot_index1 = [str(idx) for idx in plot_data1.index]
plot_index2 = [str(idx) for idx in plot_data2.index]

# atherosclerosis.expr_mean.sort_index(level=['dose','age']).plot(alpha=0)
plt.errorbar(plot_data1['range'], plot_data1["expr_mean"], 
             yerr=plot_data1["expr_std"]/2,
             marker="s", mfc='green', 
             markeredgewidth=2, capsize=4, capthick=2, 
             fmt='o-', ecolor="magenta")
plt.errorbar(plot_data2['range'], plot_data2["expr_mean"], 
             yerr=plot_data2["expr_std"]/2,
             marker="s", mfc='green', 
             markeredgewidth=2, capsize=4, capthick=2, 
             fmt='o-', ecolor="magenta")

plt.xticks(atherosclerosis['range'], atherosclerosis.index);

输出: