Error bars on grouped bar plot

조회 수: 3 (최근 30일)
Johnathan Viernik
Johnathan Viernik 2017년 1월 8일
댓글: Kien Nguyen 2019년 12월 20일
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
Kien Nguyen
Kien Nguyen 2019년 12월 20일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2017년 1월 8일
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
Frederik Dalby
Frederik Dalby 2018년 3월 13일
편집: Frederik Dalby 2018년 3월 13일
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 2019년 3월 14일
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일

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
Kien Nguyen
Kien Nguyen 2019년 12월 20일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by