Error bars on grouped bar plot

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 个评论

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

请先登录,再进行评论。

 采纳的回答

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 个评论

Thanks so much for this answer, Star Strider. I just wanted to add a small addition to your answer for R2014b and later. The previous code only worked for symmetric a matrices. I extended it so it works for asymmetric matrices.
a=[0,1,0,0;
4,3,2,1;
2,2,1,3;
1,0,0,0;
1,2,3,4];
b=[0,1,0,0;
1,2,1,1;
1,1,1,2;
1,0,0,0;
1,2,3,4];
ctrs = 1:5;
data = a;
figure(1)
hBar = bar(ctrs, data);
ctr = [];
ydt = [];
for k1 = 1:size(a,2)
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
Thanks again, Star Strider!
Noted. Thank you for your contribution!
As always, my pleasure!
Thank you for this answer and for the generalized example. This last example unfortunately won't work if you call "hold on" prior to calling bar(). For some reasons, x coordinates of individual bars get rounded in this case. Does anyone have an idea of why it would be the case? Here is the code to reproduce this behaviour:
nGroup = 5; % Number of group
nStack = 3; % Number of bars/group
% Generate some random data
se = rand(nStack, nGroup); % standard error
data = randn(nStack, nGroup); % averages
%%Working example ("hold on" after calling bar())
figure;
% Plot individual bars
hBar = bar(data');
% Get x and y position of bars
ctr = zeros(nStack, nGroup);
ydt = zeros(nStack, nGroup);
for i = 1:length(hBar)
ctr(i,:) = bsxfun(@plus, hBar(1).XData, [hBar(i).XOffset]');
ydt(i,:) = hBar(i).YData;
end
disp(ctr); % Prints x positions (double numbers)
hold on;
% Plot error bars on top of individual bar plots
errorbar(ctr', ydt', se', 'o', 'marker', 'none', 'linewidth', 2);
hold off;
%%Not Working example ("hold on" before calling bar())
figure; % Create figure
hold on;
hBar = bar(data');
ctr = zeros(nStack, nGroup);
ydt = zeros(nStack, nGroup);
for i = 1:length(hBar)
ctr(i,:) = bsxfun(@plus, hBar(1).XData, [hBar(i).XOffset]');
ydt(i,:) = hBar(i).YData;
end
disp(ctr); % Prints x positions (rounded numbers)
% Plot error bars on top of individual bar plots
errorbar(ctr', ydt', se', 'o', 'marker', 'none', 'linewidth', 2);
hold off;
This (Star Strider's post-2014 answer) happens to "work" (not error) because the matrix is square but I'm pretty sure everything is completely scrambled because the error data is rotated relative to the y data. Nonetheless, in principle it was what I was looking for so thank you.
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
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'})

请先登录,再进行评论。

更多回答(1 个)

Mary Rezaee
Mary Rezaee 2019-7-21

0 个投票

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 个评论

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

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Annotations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by