python - 使用 xerr 和 yerr 使用 matplotlib 的散点图

标签 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

我基本上想要的是这样的: scatterplot with two errorbars

所以在这个图中,每个点基本上都具有三个属性: 1.错误栏 2.错误栏 3.描述这个点代表什么。

我觉得这可以用 matplotlib 优雅地完成,虽然我尝试了一些带有错误栏的东西,但并没有完全达到我的期望。而且我还没有找到如何将字幕放在情节中的方法。

最佳答案

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

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()

enter image description here

errorbar就像 plot 一样工作.如果您想要“散点图”,则需要指定 linestyle='none' (或等效地,ls='none')。根据您的绘图,您不希望错误栏上有上限,所以我指定了 capsize=0 .同样,您似乎希望错误栏的线条相当粗,因此 elinewidth=2 .

如果除了误差线还需要标记,只需指定 marker='o' (或您喜欢的任何标记样式)到 errorbar .

annotate是在图上注释点的最简单方法。在这里,我指定注释应放置在每个测量值上方和右侧 8 个处。

关于python - 使用 xerr 和 yerr 使用 matplotlib 的散点图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10707147/

相关文章:

python - Django 的 TinyMCE

r - 在 n 个图中绘制一个数值变量与 n 个数值变量的关系图

matlab - 在 MATLAB 中使用位于圆柱体上的点的密度为圆柱体着色

r - R ggplot透明度-以其他变量为条件的alpha值

python - Matplotlib 版本

python - 可以在 Python 的绘图命令中添加条件语句吗?

python - Clojure 相当于 Python 的 "any"和 "all"函数?

python - 使用 SAS 将文件上传到 Azure

python - 使用python在文件中插入一行

python - 如何制作移动散点的动画