简体   繁体   English

matplotlib errorbar连续线图

[英]matplotlib errorbar continous line plot

I've come across this interesting issue using errorbars with matplotlib. 我在使用带有matplotlib的错误栏时遇到了这个有趣的问题。 I have two lists: "iterations", which is a list of integer values ranging from 0 to 999; 我有两个列表:“迭代”,它是一个范围从0到999的整数值的列表; and "average", which is 1000 size list with real negative values. 和“平均值”,这是1000个大小列表,带有真实的负值。

If I do not specify the "yerr" attribute , I get: 如果未指定“ yerr”属性,则会得到:

errorbar(iterations,average) 误差线(迭代次数,平均值)

img1

However, if I specify the yerr attribute, but set to 0, I get the following: 但是,如果我指定yerr属性但设置为0,则会得到以下信息:

errorbar(iterations,average,yerr=0) 误差线(迭代次数,平均值,yerr = 0)

img2

It seems obvious to me that both pictures should be the same, but the second one is made of small horizontal lines, while the first one seems continuous. 在我看来,两张图片应该相同,但是第二张图片是由小的水平线组成的,而第一张图片似乎是连续的。

The problem comes when I pass an array as yerr (size:1000, all values set to 0 except some real std- error values where index%50==0, in order not to overcrowd the image). 当我将数组作为yerr传递时,问题就来了(大小:1000,所有值都设置为0,除了一些实际的std-error值(其中index%50 == 0,以便不使图像过于拥挤))。

errorbar(iterations,average,yerr=stderr) 误差线(迭代次数,平均值,yerr = stderr)

I would like to get an image where the main line is continuous (like in the first image), but instead I get a messy image, like the second one. 我想得到一张主线是连续的图像(就像第一幅图像一样),但是我却得到了一个凌乱的图像,就像第二幅图像一样。 I've tried many things, like modifying linestyle parameters, but I still keep getting something like the first image (with the errorbars at each 50-step interval) 我已经尝试了很多事情,例如修改线型参数,但仍然不断获得类似第一张图片的信息(每隔50步的时间间隔带有误差条)

Am I doing something wrong? 难道我做错了什么? Is it possible to do what I want. 有可能做我想做的事。

Update 1 更新1

As David says in the comments, the horizontal lines appear because that's the default shape of a stderr is 0. I thought a value of 0 would depict no errorbar. 正如David在评论中所说,出现水平线是因为stderr的默认形状为0。我认为值为0不会显示任何误差线。 So I only need to avoid plotting error bars there where I was setting it to 0 (error bars only in 50, 100, 150, 200, 250... 1000). 因此,我只需要避免在将其设置为0的地方绘制误差线(误差线仅在50、100、150、200、250 ... 1000中)。

Update 2 (and SOLVED) 更新2(和解决)

Adding here the solution David proposed: David在此处添加了解决方案:

# plot all points without error bars
plot(iterations, average)
# plot errorbars for every 50th point
errorbar(iterations[::50], average[::50], yerr=stderr[::50], linestyle='None')

I just added linestyle='None' to avoid plotting the line between each yerr bar. 我只是添加linestyle ='None'以避免在每个yerr条之间绘制线。

Many thanks! 非常感谢!

Well, you asked for errorbars and you got them. 好吧,您要求提供错误栏,并且得到了它们。 Conversely, if you do not specify yerr in the argument list, the documentation of error bar states that "Vertical errorbars are plotted if yerr is not None". 相反,如果未在参数列表中指定yerr ,则错误栏文档指出“如果yerr不为None,则绘制垂直错误栏 ”。

I don't understand the purpose behind using errorbar and setting yerr=0 , but the yerr=0 errorbars show up as small horizontal lines, because of the style of an errorbar with zero vertical extend. 我不理解使用errorbar并设置yerr=0的目的,但是yerr=0错误栏显示为水平小线,这是因为垂直延伸为零的错误栏的样式。

If you want to indicate the error of your many datapoints its probably best to use a shaded background indicating the error region. 如果要指示许多数据点的错误,最好使用阴影背景指示错误区域。 This could be achieved with the fill_between function 这可以通过fill_between函数来实现

Edit: with the refined question in the comments, the plotting code could be 编辑:在注释中使用提炼的问题,绘图代码可以是

# plot all points without error bars
plot(iterations, average)
# plot errorbars for every 50th point
errorbar(iterations[::50], average[::50], yerr=error[::50])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM