Plotting Error bars using minimum and maximum values in Matlab
조회 수: 26 (최근 30일)
이전 댓글 표시
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)
Thank you in advance.
댓글 수: 0
채택된 답변
dpb
2019년 12월 29일
편집: dpb
2019년 12월 29일
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. :(
Sorry, my bad.
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.
댓글 수: 4
추가 답변 (1개)
Evangelia Antonopoulou
2022년 6월 23일
Hi! If I understood the question correctly, you want an error bar with top to be the maximum value of y and bottom to be the minimum value of y. Neither of the above aswers give you that. The 2nd answer will give you an error bar for the top the size of the maximum value, so the top value will be mean+max and not max.
I did the following:
mny=nanmean(y);
ypos=max(y)-mny;
yneg=mny-min(y);
errorbar(mouse,mny,yneg,ypos)
Here, the size of the error bar for the top value (similarly for the bottom) is max(y)-mny which will result to a top value of the error bar to be the actual max.
Hope my answer is clear :)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Errorbars에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!