How to put error bars on top of grouped bars

I am trying to use the data to make up a figure in which the error bars should be placed on top of each bar. I tried a lot but the error bars are coming on top of each other. Here is my code and the image I am getting from it. Any help would be appreciated. And its little urgent. Thanks in advance.

y1 = [2.194	2.145	2.126	2.115
      2.01	2.098	2.136	2.136
      2.1	2.105	2.111	2.115
      2.155	2.133	2.187	2.192];
err1=[0.002	0.004	0.011	0.005
      0.019	0.006	0.003	0.008
      0.008	0.007	0.011	0.007
      0.013	0.016	0.013	0.019];
bar(y1,1)
ylim ([1.8 2.4])
hold on
errorbar(y1,err1,'r.');
title('Hardened density')
grid on 
grid minor
legend ('CON','MK20','nS1','nC1','location','NW')
xlabel('Mortar Type') % x-axis label
ylabel('Density (g/m^3)') % y-axis label
set(gca,'XTickLabel',{'NF','SFI','SFII','SC-M'});

 Accepted Answer

A few tweaks of your code Assuming R2014b or later):
y1 = [2.194 2.145 2.126 2.115
2.01 2.098 2.136 2.136
2.1 2.105 2.111 2.115
2.155 2.133 2.187 2.192];
err1=[0.002 0.004 0.011 0.005
0.019 0.006 0.003 0.008
0.008 0.007 0.011 0.007
0.013 0.016 0.013 0.019];
hBar = bar(y1,1); % Return ‘bar’ Handle
for k1 = 1:size(y1,1)
ctr(k1,:) = bsxfun(@plus, hBar(1).XData, hBar(k1).XOffset'); % Note: ‘XOffset’ Is An Undocumented Feature; This Selects The ‘bar’ Centres
ydt(k1,:) = hBar(k1).YData; % Individual Bar Heights
end
hold on
errorbar(ctr, ydt, err1, '.r') % Plot Error Bars
hold off
ylim ([1.8 2.4])
title('Hardened density')
grid on
grid minor
legend ('CON','MK20','nS1','nC1','location','NW')
xlabel('Mortar Type') % x-axis label
ylabel('Density (g/m^3)') % y-axis label
set(gca,'XTickLabel',{'NF','SFI','SFII','SC-M'})
produces:

5 Comments

I still cannot fathom why TMW can't (more realistically, won't) make things like this part of the interface...I know I first complained at least 25 if not 30 year ago and they've since made it more, not less, difficult by hiding the needed property and breaking the former workaround. :(
Thanks for your great help, i thought i sent a shoutout but i jsut realized i didn't, so thanks once again for all your help.
As always, my pleasure!
Thanks for the code, but it doesn't work unless y1 & err1 are square matrices.
The matrices only have to be the same size.
Change the initial line in the loop to:
for k1 = 1:size(y1,2)
for it to work correctly.

Sign in to comment.

More Answers (1)

dpb
dpb on 26 Jun 2018
Edited: dpb on 26 Jun 2018
See my previous at Answer_302325
To make more clear by placing all in one spot...to do the bars and errorbars all that is needed is--
hBar = bar(y1,1);
xBar=cell2mat(get(hBar,'XData')).' + [hBar.XOffset]; % compute bar centers
hold on
hEB=errorbar(xBar,y1,err1,'k.');
The remainder to dress up the plot, etc., etc., ... is the same.
NB1: As the reference Answer discusses, this used the hidden(!) property of .XOffset to find where the bar centers are relative to the nominal position.
NB2: I used single color (black) above as the bars were so tiny didn't show up much otherwise; you can set them to match the bar colors as hBE is an array of handles, one to match each of hBar.

3 Comments

Thank you!! Worked perfect for me
Kewl! Guess the Answers site does help sometimes that a previous solution can be found by somebody looking...
That's nice to know, appreciate the feedback!
Thanks for your great help, i thought i sent a shoutout but i jsut realized i didn't, so thanks once again for all your help.

Sign in to comment.

Products

Release

R2016a

Community Treasure Hunt

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

Start Hunting!