Trouble adding error bars to bar graph

I am trying to add error bars to bar graph. The figure looks fine if I run it without the error bars as just bar(coh). (see attached) I want to the bars to be groups as shown. I'm wondering if that grouping is messing up the error bars.
Thank you in advance for any help!
Hope you are all staying safe and healthy Matlab community.
x = 1:12;
coh = [0.9862, 0.9773
0.971, 0.9544
0.8969, 0.6791
0.8835, 0.9051
0.8558, 0.6727
0.6727, 0.8641];
errhigh = [0.0083, 0.0137
0.0137, 0.0274
0.0611, 0.1797
0.0688, 0.0563
0.1426, 0.1829
0.0875, 0.0799];
errlow = [0.0208, 0.034
0.043, 0.0661
0.1382, 0.3105
0.1533, 0.1288
0.1827, 0.3135
0.1879, 0.1743];
figure
bar(x,coh)
%bar(coh)
hold on
er = errorbar(x,coh,errlow,errhigh);
er.Color = [0 0 0];
er.LineStyle = 'none';
hold off

3 Comments

Bonus points if anyone can tell me how to shade it like this (this is the quick and dirty excel version):
In 40 years, TMW has yet to provide any decent hatching... :(
There are some submissions on File Exchange; all that I've used have warts of various severity; and whether any will work with bar I've never tried/looked.
Dang! I might hold off on that detail for now. The error bar issue is more critical at the moment. Thank you for the heads up!

Sign in to comment.

 Accepted Answer

dpb
dpb on 17 Mar 2020

9 Comments

It worked! Now I just need to figure out better shading.
x = [0.8571, 1.1429, 1.8571, 2.1429, 2.8571, 3.1429, 3.8571, 4.1429, 4.8571, 5.1429, 5.8571, 6.1429]
coh = [0.9862, 0.9773, 0.971, 0.9544, 0.8969, 0.6791, 0.8835, 0.9051, 0.8558, 0.6727, 0.6727, 0.8641];
errhigh = [0.0083, 0.0137, 0.0137, 0.0274, 0.0611, 0.1797, 0.0688, 0.0563, 0.1426, 0.1829, 0.0875, 0.0799];
errlow = [0.0208, 0.034, 0.043, 0.0661, 0.1382, 0.3105, 0.1533, 0.1288, 0.1827, 0.3135, 0.1879, 0.1743];
figure
bar(x,coh)
hold on
er = errorbar(x,coh,errlow,errhigh);
er.Color = [0 0 0];
er.LineStyle = 'none';
hold off
dpb
dpb on 17 Mar 2020
Edited: dpb on 17 Mar 2020
Hard coding the coordinates is not the right way nor what those links show how to do...keep the original bar() by groups.
In the above you've now lost the two bar handles for one and so the color between the two groups is also gone.
hB=bar(coh); % NB: THIS IS THE FIRST 2D ARRAY COH, NOT 1D VECTOR!!!!
hold on
X=cell2mat(get(hB,'XData')).' + [hB.XOffset];
hEB=errorbar(X,coh,errlow,errhigh,'.'); % DITTO ON DATA!!!!
set(hEB,{'Color'},get(hB,{'FaceColor'}))
produces
Your alternative above forces using CData color for every bar individually; the above lets you set FaceColor by bar group. There are examples of both in the documentation; there is, however, still no hatching pattern functionality in MATLAB but you can certainly pick whatever color combos you so choose and mess with opacity, etc., ... to end up with a pleasing result.
Thank you. I really appreciate your help with this.
The color for errorbars above is just illustrative of how to set to something other than just one of the presets; using the face color of the bar isn't a good choice, obviously as it makes the lower bar disappear...
Heidi Hirsh
Heidi Hirsh on 18 Mar 2020
Edited: Heidi Hirsh on 18 Mar 2020
Thanks again!
Can I go through and assign the color of each bar individually? I want to use colors to discriminate between the bars in each pair but also between different groups of paired bar. Right now I think I can only assign colors to the two groups.
Again, don't think that's the best way...one way is:
cohg=[coh(1:2,:) nan(2,2) nan(2,2);
nan(2,2) coh(3:4,:) nan(2,2);
nan(2,2) nan(2,2) coh(5:6,:)];
bar(cohg)
produces
Uses the fact that Matlab plot functions silently ignore NaN and so setting up 2*nGroups --> 6 columns instead of grouping all only by the two columns with placeholders for no members of each group simulates the three groupings in the alternate figure--and gives different colors for each group automagically.
Another way to do something similar would be to set distinct X values
figure
bar([1:2],coh(1:2,:))
hold on
for i=2:3,ix=[2*i-1 2*i];bar(ix,coh(ix,:));end
xticks(1.5:2:6)
xticklabels({'PH v. DO','PH v. T','DO v. T'})
Of course, save bar and axes handles for more programmatic updating, but there's more than one way to skin the cat... :)
Thanks for the tips! Unfortunately what I actually need to do is color the odd and even pairs of columns the same. This is because the first pair in each set (3 sets: pH v. DO, pH v. T, DO v. T) are from a different depth than the second pair (near-surface versus bottom). Does that make sense? I attached an excel version of what I am trying to do.
I want to be able to see the difference between depth (red v. blue in the excel plot) and between frequency (solid verus opaque coloring). This is why I thought I might have to color each bar individually. But I'm not sure how to do that.
Thank you!
Rearrange your groups/x values to match the groups you want highlighted the same.
I'm not sure I understand what you mean

Sign in to comment.

More Answers (0)

Asked:

on 17 Mar 2020

Commented:

on 24 Mar 2020

Community Treasure Hunt

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

Start Hunting!