How to add error bars on bar graph with groups?
Show older comments
Hi all,
I'm trying to create a bar graph with error bars.
So far, my code is:
c = categorical({'CH','VC','GC','OC','BC','SC'});
c = reordercats(c,{'CH','VC','GC','OC','BC','SC'});
y = [707, 599; 464 444; 522 475; 566 346; 1329 1384; 459 498];
std_dev = [321 271; 233 91; 202 132; 0 173; 410 850; 179 122];
title('Title'); xlabel('x-label'); ylabel('y-label');
figure
hold on
bar(c,y)
errorbar(y,std_dev,'.')
The resulting graph is attached. The error bars appear to stack on top of each other, and are between the the two bars in each pair. I'm unsure how to make each error bar match with individual bars.
If it's helpful, the data in y is annual, where the first number (i.e. 707, 464, 522, 566) is Year 1 and the second number (i.e. 599, 444, 475, 346) is Year 2. The groups are further broken down so that the first three groups (c = CH, VC, GC) are one set and the last three (c = OC, BC, SC) are a second set. If possible, I would also like to have the Year 1 and Year 2 be different colors, as well as the two sets. So, for example, 707 = red, 599 = blue, 459 = green, 498 = orange.
Any help getting started is greatly appreciated!
Cheers
2 Comments
Star Strider
on 11 May 2018
What version of MATLAB are you using?
The categorical data type was introduced in R2013b. A significant change in handle graphics was introduced in R2014b.
Accepted Answer
More Answers (1)
See <Errors on Bar> to retrieve the locations of the various bars' offsets from the nominal group center.
I would urge to add your complaint to the making of such obviously-desired features so difficult in BAR() by submitting enhancement request/support request. As noted, why TMW would hide the necessary properties is beyond ken. Shows extreme lack of forethought at best; in reality the whole interface ought to be redesigned; it's a klutz.
ADDENDUM
Had a few minutes; hmm....I see the XTick values from the barplot object are returned as the categorical values; not useful for errorbar. OK, go at it this way...
hBar=bar(double(c),y); % plot against the underlying value
hAx=gca; % handle to the axes object
hAx.XTickLabel=categories(c); % label by categories
hold on
X=cell2mat(get(hBar,'XData')).'+[hBar.XOffset]; % compute bar locations
hEB=errorbar(X,y,std_dev,'.') % add the errorbar
for i=1:length(hEB) % See Note...
hEB(i).Color=hBar(i).Facecolor;
end
Above yields

NB: There's a syntax to set multiple handles w/o the loop but its complicated enough I can never get it right the first time or two; see the doc for set for details if want to eliminate the loop as a project... :)
set(hEB,{'Color'},get(hBar,'FaceColor'))
I always forget you have to turn the property name into cellstr to do multiple objects...
NB2: The key feature here is the use of the hidden 'XOffset' property to retrieve the actual midpoint of the bars in each group--this code will work correctly irregardless of the number of groups/number per group, the other Answer uses an approximate constant offset that is specific for only six and two and will have to be modified manually. It's also not quite the right value; close, but not what ML uses internally --
>> [hBar.XOffset] % internal offset
ans =
-0.1429 0.1429
>>
NB3: This also automates setting the colors to match precisely.
NB4: Reiterate to beat on TMW for how difficult is to use BAR() and this about the way categorical is treated just makes it worse...
4 Comments
DrWooo
on 13 May 2018
"I was unable to fully follow this code..."
In what regards and where and why? It's reasonably straightforward after one knows about the hidden property...if you're interested in that level of detail, I discovered it via Yair's "Undocumented Matlab" site; there's another Q? here in Answers that showed how to uncover; I'll see if can find and post the link instead of duplicating yet again...
Well, I don't find that posting I was sure had done; here's link to Yair's blog page with background. <Y Altman's blog undocumented object properties>
Kimberly Stevens
on 13 Jan 2020
This was a great solution; I've been looking all over for a solution to this problem, and yours is by far the best. Thank you much!
dpb
on 13 Jan 2020
I would suggest to add your voice via contact link on enhancement need and the overall bar() interface, too. Maybe eventually TMW will finally do something about it.
Categories
Find more on Data Distribution Plots 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!