Bar(matrix) with errorbars on the same figure
Show older comments
Hi
I'm trying to plot bar with errorbars on the same figure. I tryed but it doesn't seem to work. Code: a=<4x4 double> = [0,1,0,0; 4,3,2,1; 2,2,1,3; 1,0,0,0] b=<4x4 double> = [0,1,0,0; 1,2,1,1; 1,1,1,2; 1,0,0,0] bar(a); hold on;errorbar(a,b,'.'); the answer is

How to put errorbar on each bar?
Answers (1)
Star Strider
on 30 Dec 2016
0 votes
4 Comments
fu-hua yang
on 2 Jan 2017
Edited: fu-hua yang
on 2 Jan 2017
Star Strider
on 2 Jan 2017
My pleasure!
This works for me, and seems to produce the correct plot.
The Code:
a=[0,1,0,0;
4,3,2,1;
2,2,1,3;
1,0,0,0];
b=[0,1,0,0;
1,2,1,1;
1,1,1,2;
1,0,0,0];
ctrs = 1:4;
data = a;
figure(1)
hBar = bar(ctrs, data);
for k1 = 1:size(a,1)
ctr(k1,:) = bsxfun(@plus, hBar(1).XData, [hBar(k1).XOffset]');
ydt(k1,:) = hBar(k1).YData;
end
hold on
errorbar(ctr, ydt, b, '.r')
hold off
The Plot:

fu-hua yang
on 3 Jan 2017
Star Strider
on 3 Jan 2017
My pleasure!
I assumed you were using R2014b or later. I can no longer run this code (archived from a 2014 Answer), so I will let you adapt it. The loop is different for R2014a and earlier:
load('Mean.mat') % Mean contains all mean values and consists of 5 rows
load('Stddeviation.mat')
x = [time]; % define x-axis
y = [IC1,IC2,IC3,IC4]; % define y-axis
e = [sd1,sd2,sd3,sd4,sd5,sd6]'; % define standard deviation
figure
hBars = bar(y,1.0,'grouped');
set(gca,'XTickLabel',{'Baseline','0,1','0.2','0.3','0.4','0.5'})
% set ticks and tick labels
xlabel('Time')
ylabel('Impulse')
hold on
for k1 = 1:4
hb = get(get(hBars(k1),'Children'), 'XData');
midbar = mean(hb);
errorbar(midbar, y(:,k1), e(:,k1), '.') % plotting errors
end
title('Impulse');
legend('a','b','c','d','Location','SouthEastOutside')
% put in lower right
box off
hold off
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!