# Plotting Error bars using minimum and maximum values in Matlab

29 views (last 30 days)
Maimuna Mohamud on 29 Dec 2019
Commented: dpb on 30 Dec 2019
Hi,
I'm trying to plot an x by y graph. There are several y values for each x value so I want to plot the average of these with error bars. I want each error bar to have the largest value as the upper limit and the smallest value as the lower limit for every point plotted. How do I do this? (I just can't seem to get my head round the help already available on 'errorbar' in matlab)

dpb on 29 Dec 2019
Edited: dpb on 29 Dec 2019
Just make a vector of max(y), min(y) the same length as x and add/subtract to the mean for the error bar lengths.
Since both functions are vectorized to accept arrays and return (by default) the result by column, it's just one line each assuming you organize your data by column.
mny=mean(y); % the y means (assumes columns are observations)
ypos=mny+max(y); % for errorbar +ive
yneg=mny-min(y); % for errorbar -ive
errorbar(x,y,yneg,ypos,'x') % plot, no line, 'x' marker
ERRATUM:
Actually, the above isn't quite correct...the error bar lengths are the yneg, ypos arguments; errorbar() itself will take care of the arithmetic. And, to boot, I didn't use the mean() as the y. All in all, 0 for 3. :(
mny=mean(y); % the y means (assumes columns are observations)
ypos=max(y); % for errorbar +ive
yneg=min(y); % for errorbar -ive
errorbar(x,mny,yneg,ypos,'x') % plot, no line, 'x' marker
If you don't have need otherwise for min/max values, you could just compute and pass inside the errorbar call and dispense with the yneg, ypos temporary variables entirely.

Show 1 older comment
dpb on 29 Dec 2019
Actually, the above isn't quite correct...the error bar lengths are the yneg, ypos arguments; errorbar() itself will take care of the arithmetic.
Just pass max,min as the errors, not the actual endpoints....sorry, my bad. Will correct answer, too.
Maimuna Mohamud on 29 Dec 2019
Oh no worries - thank you again!
dpb on 30 Dec 2019
You're too kind! :)