how to add errorbars

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

aviran
aviran on 19 Jun 2014
Edited: Star Strider on 19 Jun 2014
thanks! you saved my day
i change a little bit the data and now its not working
mw_150=[58,14.5,19,8.5];
mw_100=[54.5,10.42,17.4,17.7];
control=[6.5,7.15,41.9,44.3];
std_150=[2.34,1.16,1.33,0.9];
std_100=[1.9,0.83,1.01,1.09];
std_control=[0.68,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
how can i fix this?
After some outright experimenting, this works:
mw_150=[58,14.5,19,8.5];
mw_100=[54.5,10.42,17.4,17.7];
control=[6.5,7.15,41.9,44.3];
std_150=[2.34,1.16,1.33,0.9];
std_100=[1.9,0.83,1.01,1.09];
std_control=[0.68,0.71,1.72,1.77];
mw_data = [mw_150; mw_100; control];
hb = bar([1 2 3],mw_data)
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
sclf = 0.18;
mv = mean(1:size(mw_data,2));
% sclv = ([1:size(mw_data,2)]-mv)*sclf+1
hold on
for k1 = 1:size(mw_data,1)
errorbar(([1:4]-mv)*sclf+k1 ,mw_data(k1,:), errbar(k1,:), '.k', 'LineWidth',2)
end
hold off
If you change anything, it should be robust enough to work except for the variable I call ‘sclf’. That 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. So if you change anything else, experiment with ‘sclf’ until it looks decent.
aviran
aviran on 19 Jun 2014
i think that i got confused and i didnt explaind it right to you.
i want to have in the "x axis" 4 ranges
1. A.R<1.5
2. 1.5<A.R<2.5
3. 2.5<A.R<3.5
4.A.R>3.5
and in each range i want to have 3 colums one for "150 mJ/cm^2" the second is for "100 mJ/cm^2" and the third is for" control" and of cours in each colum i need errorbar
sorry for disturbing you but if you can help me (hopefully for the last time).
thank you very much!
Easily fixed. The caution on ‘sclf’ still holds!
The code:
mw_150=[58,14.5,19,8.5];
mw_100=[54.5,10.42,17.4,17.7];
control=[6.5,7.15,41.9,44.3];
std_150=[2.34,1.16,1.33,0.9];
std_100=[1.9,0.83,1.01,1.09];
std_control=[0.68,0.71,1.72,1.77];
mw_data = [mw_150; mw_100; control]';
hb = bar([1:size(mw_data,1)],mw_data)
legend('150mJ/cm2','100mJ/cm2','control');
set(gca, 'FontSize',12,'XTick',[1:size(mw_data,1)],'XTickLabel',{'A.R<1.5','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
sclf = 0.225;
mv = mean(1:size(mw_data,2));
% sclv = ([1:size(mw_data,2)]-mv)*sclf+1
hold on
for k1 = 1:size(mw_data,1)
errorbar(([1:size(mw_data,2)]-mv)*sclf+k1 ,mw_data(k1,:), errbar(k1,:), '.k', 'LineWidth',2)
end
hold off
You had the same three 'XTickLabel' designations, so I assumed they didn’t change, and you were adding another bar to each of them.
Note that ‘sclf’ again changed experimentally. (I still can’t figure out how those bar centres are calculated.)
aviran
aviran on 19 Jun 2014
thanks it works!
i have a little question : when i copy-paste the graph to word/excel the errorbars looks strange. maby you know why ? also in matlab only if you enlarge the figure you can see the errorbar perfectlly.
i am attaching the excel file
Star Strider
Star Strider on 19 Jun 2014
Edited: Star Strider on 19 Jun 2014
My pleasure!
From what I understand from others who have exported MATLAB graphics to other applications, sometimes they do look strange after exporting them. Another File Exchange contribution, export_fig seems to solve those problems. I’ve not used it often, but I’ve always been impressed with its results.
If you’re exporting the figure for publication, you have to adhere to the journal’s style guide, so be mindful of those requirements. If you’re exporting it for other purposes, your best option is probably as a ‘.png’ file. Nevertheless, I suggest you use export_fig to avoid most — if not all — potential problems for exported MATLAB graphics.
dpb
dpb on 19 Jun 2014
Looks pretty much the same in Excel here other than the ylabel is a little funky.
The error bar top/bottom hats are, indeed, pretty miniscule and I don't see any property to let you control how long they are. Star tried to help some by setting the width parameter to make them a little darker but that doesn't do anything for the length of the bounding lines.
Think you're stuck there unless you delve into the code for errorbar itself and figure out where they're drawn or just draw lines yourself--it's really not that much more involved than the machinations already doing.
aviran
aviran on 19 Jun 2014
thanks
i think that the problem is not the exporting because also in the figure in matlab the errorbars look strange but i will try the export_fig
and again, thanks!
My pleasure!
(The most sincere expression of appreciation here on MATLAB Answers is to Accept the Answer that most closely solves your problem.)
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
@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

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!