How to put error bars on top of grouped bars
Show older comments
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
More Answers (1)
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
Oleksandra Romanyshyn
on 25 Jul 2019
Thank you!! Worked perfect for me
dpb
on 25 Jul 2019
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!
Amardeep Singh
on 26 Jul 2019
Categories
Find more on Errorbars in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!