Reputation: 2263
I'm trying to get: a nice bar graph of groups. I'd like error bars on each bar like this (or similar at your discretion):
I have the means:
ff =
Medical eq Patient Hygiene Near bed Far bed
Direct 1.2759 0.9253 0.6092 1.0460 1.3276
Housekeeping 1.0833 0.2500 0.5833 2.1667 1.3333
Mealtimes 0 0.3000 0.7000 1.4500 0.5000
Medication 0.3694 0.7838 0.8919 1.5495 1.0541
Misc 0.1059 0.1294 0.4118 0.8000 0.8353
Personal 0.0370 0.4074 0.8148 1.2593 0.7037
the standard deviations
ee =
2.0411 1.1226 0.8378 1.5007 1.3776
1.3114 0.4523 0.6686 2.4058 1.1547
0 0.7327 1.3803 2.1392 0.6070
0.7499 0.9186 1.0300 1.2844 1.3062
0.4371 0.3712 0.7605 1.0212 0.6699
0.1925 0.6939 1.6417 3.5582 1.5644
instead I get this:
by using:
bb=bar(ff'); hold all
data=repmat([1:6]'*ones(5,1)',1,1)
er=errorbar(data, ff, ee, '.')
I read I have to find the centre of each bar? That's crazy! Any workaround?
Upvotes: 3
Views: 17917
Reputation: 351
the centre of each bar can be obtained by
x = get(get(h(i),'children'),'xdata');
barsx=mean(x,1);
barsx gives the center for every ith element of every bar subset.
h=bar(bars)
for i=1:6
x = get(get(h(i),'children'),'xdata');
barsx(1:6,i)=mean(x,1)
end
hold all
h=errorbar(barsx,bars,barsvar)
to have the errors having the same colors as bar:
figure()
h=bar(bars)
col=[0 0 1;0 1 0;1 1 0; 1 1 1; 0 0 0; 0 1 1];
colormap([0 0 1;0 1 0;1 1 0; 1 1 1; 0 0 0; 0 1 1])
hold all
for i=1:6
x = get(get(h(i),'children'),'xdata')
barsx=mean(x,1)
h1=errorbar(barsx',bars(1:6,i),barsvar(1:6,i),'color',col(i,:))
set(h1,'linestyle','none')
end
Upvotes: 3
Reputation: 14261
It's really not that crazy!
You can just use errorbar
with the data that you have readily available.
This code computes the correct locations, by adding 1 for every group and 1/7 for each bar within a group.
for i = 1:5
j = 1:6;
x = -0.5 + i + 1/7 * j;
errorbar(x, ff(j,i), ee(j,i), '.');
end
Results in:
(Leaving out the labels, but otherwise this seems pretty similar to what you were looking for)
Upvotes: 2