
How to put error bars on top of grouped bars
3 views (last 30 days)
Show older comments
Amardeep Singh
on 26 Jun 2018
Commented: Star Strider
on 13 Dec 2020
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'});
0 Comments
Accepted Answer
Star Strider
on 26 Jun 2018
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
Krishnashish Bose
on 13 Dec 2020
Thanks for the code, but it doesn't work unless y1 & err1 are square matrices.
Star Strider
on 13 Dec 2020
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.
More Answers (1)
dpb
on 26 Jun 2018
Edited: dpb
on 26 Jun 2018
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
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!
See Also
Categories
Find more on Creating, Deleting, and Querying Graphics Objects 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!