Clear Filters
Clear Filters

how to add errorbars

27 views (last 30 days)
aviran
aviran on 19 Jun 2014
Commented: Star Strider on 6 Jun 2022
i want to add errorbars to this graph (the error bar are "std_150,std_100 and std_control" in the code).
thanks
the code:
mw_150=[14.5,19,8.5];
mw_100=[10.42,17.4,17.7];
control=[7.15,41.9,44.3];
std_150=[1.16,1.33,0.9];
std_100=[0.83,1.01,1.09];
std_control=[0.71,1.72,1.77];
bar([1 2 3],[mw_150' mw_100' control'])
legend('150mJ/cm2','100mJ/cm2','control');
set(gca, 'FontSize',12,'XTick',[1 2 3 ],'XTickLabel',{'1.5<A.R<2.5','2.5<A.R<3.5','A.R>3.5' });
ylabel('Relative precentage')
  2 Comments
dpb
dpb on 19 Jun 2014
Not sure exactly how to envision error "bars" on a bar chart...can you describe what you think it should look like?
Only thoughts come to mind are either use a stacked bar but doesn't really seem like it would work well or use errorbar with the adjust x- coordinates for the bars and no line on the plotted line. I've not tried to see if can convince errorbar to draw the bars but not the trend line directly or whether it would take finding/setting appropriate line handle linestyle property.
aviran
aviran on 19 Jun 2014
it should look like in the attached picture

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 19 Jun 2014
Edited: Star Strider on 19 Jun 2014
This File Exchange contribution is highly regarded: Bar Chart with Error Bars.
EDIT —
If you’d rather have it look like your attached figure, this code will work:
mw_150=[14.5,19,8.5];
mw_100=[10.42,17.4,17.7];
control=[7.15,41.9,44.3];
std_150=[1.16,1.33,0.9];
std_100=[0.83,1.01,1.09];
std_control=[0.71,1.72,1.77];
hb = bar([1 2 3],[mw_150' mw_100' control'])
legend('150mJ/cm2','100mJ/cm2','control');
set(gca, 'FontSize',12,'XTick',[1 2 3 ],'XTickLabel',{'1.5<A.R<2.5','2.5<A.R<3.5','A.R>3.5' });
ylabel('Relative precentage')
errbar = [std_150; std_100; std_control]; % CREATE ‘errbar’ MATRIX
yd = [mw_150' mw_100' control']';
hold on
for k1 = 1:3
errorbar([1:3]+.22*(k1-2), yd(k1,:), errbar(k1,:), '.k', 'LineWidth',2)
end
hold off
  11 Comments
Yeasir Mohammad Akib
Yeasir Mohammad Akib on 6 Jun 2022
Hello State Strider,
Can you please expalin the meaning of this line:
for k1 = 1:size(mw_data,1)
errorbar(([1:4]-mv)*sclf+k1 ,mw_data(k1,:), errbar(k1,:), '.k', 'LineWidth',2)
end
The .k portion
Star Strider
Star Strider on 6 Jun 2022
@Yeasir Mohammad Akib — It plots black dots, I believe to avoid connecting the error bars with lines. (This is nearly 8 year-old code. I do not remember what I was thinking back then when I wrote it.)

Sign in to comment.

More Answers (1)

dpb
dpb on 19 Jun 2014
Edited: dpb on 19 Jun 2014
...scales the matrix to put the errorbars in the middle of the plotted bars, and I have no idea how MATLAB determines where it puts those bars. The handle graphics don’t say, because all the 'XData' values are the same for all variables. I cannot find anything in the documentation that alludes to where they are stored or how they are calculated...
Got's to go handle-diving Star--they're not identified per se, but you can compute the location by retrieving the XData property from the barseries object patch...
For the sample above for the first handle, one can find
>> xtik=get(get(hb(1),'children'),'xdata')
xtik =
0.6545 1.6545 2.6545
0.6545 1.6545 2.6545
0.8000 1.8000 2.8000
0.8000 1.8000 2.8000
>> mean(xtik(1:2:end,:))
ans =
0.7273 1.7273 2.7273
>>
By comparison, your heuristic values were
>> ([1]-mv)*sclf+[1:3]
ans =
0.7300 1.7300 2.7300
>>
Not bad... :)
  2 Comments
Star Strider
Star Strider on 19 Jun 2014
Thank you! I went handle-diving, but apparently not deep enough.
dpb
dpb on 19 Jun 2014
No problem...came up with this w/ a poster's help when labeling a bar series w/ values. I'd always used a heuristic based on a the 'Width' parameter before but it wasn't completely reliable, either. This returns the real deal of where actually draws the patch object so is dead on...

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!