散点图与xerr和yerr与matplotlib

tar*_*sch 7 python plot matplotlib

我希望可视化两个阵列的位置.我的表看起来像这样

    Number          Description      value_1  value_2  err_1 err_2
         1          descript_1       124.46   124.46   22.55  54.2
         2          Descript_2       8.20     50.2     0.37   0.1 
         3          Descript_2       52.55    78.3     3.77   2.41
         4          Descript_2       4.33     778.8    0.14   1.78
Run Code Online (Sandbox Code Playgroud)

我基本上想要的是这样的: 带有两个错误栏的散点图

所以在这个情节中,每个点基本上都有三个属性:1.xerror bar 2. yerror bar 3.描述这一点代表什么.

我有一种感觉,这可以用matplotlib优雅地完成,虽然我尝试了一些错误栏的东西,并没有完全给我我期望的东西.我还没有找到如何将标题放在情节中.

Joe*_*ton 8

听起来你想要这样的东西?

import matplotlib.pyplot as plt

x = [124.46, 8.20, 52.55, 4.33]
y = [124.46, 50.2, 78.3, 778.8]
xerr = [54.2, 0.1, 2.41, 1.78]
yerr = [22.55, 0.37, 3.77, 0.14]

descrip = ['Atom 1', 'Atom 2', 'Atom 3', 'Atom 4']


plt.errorbar(x, y, xerr, yerr, capsize=0, ls='none', color='black', 
            elinewidth=2)

for xpos, ypos, name in zip(x, y, descrip):
    plt.annotate(name, (xpos, ypos), xytext=(8, 8), va='bottom',
                textcoords='offset points')

plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

errorbar工作就像plot.如果你想要一个"分散"图,那么你需要指定linestyle='none'(或等效ls='none').根据您的绘图,您不希望在错误栏上设置上限,所以我已经指定了capsize=0.同样,你似乎想要为错误栏提供相当粗的线条elinewidth=2.

如果除了错误栏之外还需要标记,只需指定marker='o'(或任何您想要的标记样式)即可errorbar.

annotate是在图上注释点的最简单方法.在这里,我已经指定注释应该放在每个测量的上方和右侧8 个点.