I've come across this interesting issue using errorbars with matplotlib. I have two lists: "iterations", which is a list of integer values ranging from 0 to 999; and "average", which is 1000 size list with real negative values.
If I do not specify the "yerr" attribute , I get:
errorbar(iterations,average)
However, if I specify the yerr attribute, but set to 0, I get the following:
errorbar(iterations,average,yerr=0)
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).
errorbar(iterations,average,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)
Am I doing something wrong? Is it possible to do what I want.
Update 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. 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).
Update 2 (and SOLVED)
Adding here the solution David proposed:
# 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.
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".
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.
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
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])