Python Pylab散点图误差条(每个点上的误差是唯一的)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22364565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:48:36  来源:igfitidea点击:

Python Pylab scatter plot error bars (the error on each point is unique)

pythonmatplotlib

提问by user3412782

I am attempting a scatter plot of 2 arrays for which I have a third array containing the absolute error (error in y direction) on each point. I want the error bars to between (point a - error on a) and (point a + error on a). Is there a way of achieving this with pylab and if not any ideas on how else I could do it?

我正在尝试绘制 2 个数组的散点图,其中我有第三个数组,其中包含每个点上的绝对误差(y 方向的误差)。我希望误差线介于(a - a 上的错误)和(a 上的 a + 错误)之间。有没有办法用 pylab 实现这一点,如果没有关于我还能怎么做的任何想法?

采纳答案by 2705114-john

>>> import matplotlib.pyplot as plt
>>> a = [1,3,5,7]
>>> b = [11,-2,4,19]
>>> plt.pyplot.scatter(a,b)
>>> plt.scatter(a,b)
<matplotlib.collections.PathCollection object at 0x00000000057E2CF8>
>>> plt.show()
>>> c = [1,3,2,1]
>>> plt.errorbar(a,b,yerr=c, linestyle="None")
<Container object of 3 artists>
>>> plt.show()

where a is your x data b is your y data c is your y error if any

其中 a 是您的 x 数据 b 是您的 y 数据 c 是您的 y 错误(如果有)

note that c is the error in each direction already

请注意, c 已经是每个方向的误差

回答by MSeifert

This is almost like the other answer but you don't need a scatterplot at all, you can simply specify a scatter-plot-like format (fmt-parameter) for errorbar:

这几乎就像另一个答案,但您根本不需要scatter绘图,您可以简单地为以下指定散点图格式(fmt-parameter)errorbar

import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [1, 4, 9, 16]
e = [0.5, 1., 1.5, 2.]
plt.errorbar(x, y, yerr=e, fmt='o')
plt.show()

Result:

结果:

enter image description here

在此处输入图片说明

A list of the avaiable fmtparameters can be found for example in the plotdocumentation:

fmt例如,可以在plot文档中找到可用参数的列表:

character   description
'-'     solid line style
'--'    dashed line style
'-.'    dash-dot line style
':'     dotted line style
'.'     point marker
','     pixel marker
'o'     circle marker
'v'     triangle_down marker
'^'     triangle_up marker
'<'     triangle_left marker
'>'     triangle_right marker
'1'     tri_down marker
'2'     tri_up marker
'3'     tri_left marker
'4'     tri_right marker
's'     square marker
'p'     pentagon marker
'*'     star marker
'h'     hexagon1 marker
'H'     hexagon2 marker
'+'     plus marker
'x'     x marker
'D'     diamond marker
'd'     thin_diamond marker
'|'     vline marker
'_'     hline marker