Error bars on grouped bar plot

5 views (last 30 days)
Hi, I would like to place error bars for the following grouped bar plot. I tired different solution on the web but couldn't find the one that worked... Any help would be appreciated! Thanks
  1 Comment
Kien Nguyen
Kien Nguyen on 20 Dec 2019
Hi Jahnathan,
I am trying to plot bar chart like your image, but I do not know how to read the bar values and error values from the excel file. For example:
1 1.1 1.3 1.5 1.8 0.2 0.23 0.4 0.3
2 1.5 1.2 1.6 2.0 0.22 0.27 0.34 0.2
3 1.4 1.9 1.0 1.5 0.26 0.18 0.2 0.25
Here x = column 1 (1, 2, 3)
y = columns 2, 3, 4 (1.1 1.3 1.5 1.8
1.5 1.2 1.6 2.0
1.4 1.9 1.0 1.5
and error bars = columns 5,6,7
Do you know how to read these data to plot bar chart?
Thanks,
Kevin

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 8 Jan 2017
You didn’t say what you tried or what version of MATLAB you’re running.
This will work for R2014b and later:
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
Here, ‘a’ are the bars, and ‘b’ are the error bars.
A different approach is necessary for R2014a and earlier:
figure(1)
hBar = bar(xval,data); % Plot Data, Get Handle
set(hBar(1), 'FaceColor', cmap(2,:)) % Colour First Bar Set
set(hBar(2), 'FaceColor', cmap(3,:)) % Colour First Bar Set
set(gca, 'YLim', [870 1080]) % Set Y-Axis Limits
hold on
for k1 = 1:length(hBar) % Loop: Plots Error Bars
hb = get(get(hBar(k1),'Children'), 'XData');
midbar = mean(hb);
errorbar(midbar, data(:,k1), errs(:,k1), '.') % plotting errors
sigbarx(k1,:) = midbar; % Use To Plot Significance Bars
end
I can no longer run the R2014a code, so you will have to experiment with the concepts with your data.
In both code examples, the errorbar plotting occurs in the for loop.
  6 Comments
Frederik Dalby
Frederik Dalby on 13 Mar 2018
Edited: Frederik Dalby on 13 Mar 2018
Hi, I have tried out the script, which works great when the x-axis is numeric. However i cant get it to work when i want a categorical x-axis. It gives me an error from the bsxfun: "Error using bsxfun Operands must be numeric arrays."
My script looks like Star Strider's except ctrs= categorical(["hey", "what", "is", "this","kind"]);
do you have any suggestions to make it work? I hope you can help
Caleb Begly
Caleb Begly on 14 Mar 2019
Categorical variables for the group names make this not work. I tried a variety of different things to get them to work and finally found that the easiest way is to just let it use numbers for the groups, and then just override them using XTickLabel. So in your case:
set(gca, 'XTickLabel', {'hey','what','is','this','kind'})

Sign in to comment.

More Answers (1)

Mary Rezaee
Mary Rezaee on 21 Jul 2019

Hi everyone. Thanks for your helpful answers. I tried your code ‘Star Strider’, but I ran into a problem. “XOffset” doesn’t exist! While reaching that line of the code, I get an error which says no appropriate method, property of field “XOffset” for class ‘matlab.graphics.chart.primitive.Bar’ I am using Matlab R2017. Any help would be really appreciated.

  1 Comment
Kien Nguyen
Kien Nguyen on 20 Dec 2019
Hi,
Anyone can help me. I am a beginner.
I want to plot bar chart like Johnathan's image, but I do not know how to read the bar values and error values from the excel file. For example:
1 1.1 1.3 1.5 1.8 0.2 0.23 0.4 0.3
2 1.5 1.2 1.6 2.0 0.22 0.27 0.34 0.2
3 1.4 1.9 1.0 1.5 0.26 0.18 0.2 0.25
Here x = column 1 (1, 2, 3)
y = columns 2, 3, 4 (1.1 1.3 1.5 1.8
1.5 1.2 1.6 2.0
1.4 1.9 1.0 1.5
and error bars = columns 5,6,7
Do you know how to read these data to plot bar chart?
Thanks,
Kevin

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!