Help with Errorbars on Bar Graph

조회 수: 4 (최근 30일)
Jordyn Shaw
Jordyn Shaw 2021년 2월 22일
댓글: Star Strider 2021년 2월 23일
I created a bar graph using the following:
AngleFlex90 = [95 99; 93.33 80; 95.67 99.33]; AngleError = [8.9 6.6; 7.6 5; 4 1.2]; bar(AngleFlex90, 'grouped') set(gca, 'XTicklabel',{'Subject 1' 'Subject 2' 'Subject 3'})
hold on title('Elbow Flexion Angle for 3 Subjects with a Goal of 90 Degrees','FontSize',14, "FontWeight","bold") xlabel('Subject','FontSize',12, "FontWeight","bold") ylabel('Elbow Angle (deg)','FontSize',12, "FontWeight","bold") legend('Baseline','Experimental',"Location","northeast") ylim([0 130]) yline (90)
errorbar(AngleFlex90,AngleError,'k', "LineStyle",'none', "LineWidth",1);
The figure that came up had errorbars between each set of bars rather than in the centre of them. How do I code to have the errorbars in the centre of each bar?
I tried using ngroups and nbars codes that I found on the internet but non of them made sense to me because I’ve never coded until recently.
Thanks for any help you can give.
  댓글 수: 1
Jordyn Shaw
Jordyn Shaw 2021년 2월 22일
This is what I get using the current code but I need to centre my individual errorbars.

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

채택된 답변

Star Strider
Star Strider 2021년 2월 22일
Try this:
AngleFlex90 = [95 99; 93.33 80; 95.67 99.33];
AngleError = [8.9 6.6; 7.6 5; 4 1.2].';
figure
hBar = bar(AngleFlex90, 'grouped');
set(gca, 'XTicklabel',{'Subject 1' 'Subject 2' 'Subject 3'})
hold on
for k1 = 1:size(AngleFlex90,2)
ctr(k1,:) = bsxfun(@plus, 1:numel(hBar(k1).XData), hBar(k1).XOffset'); % Note: ‘XOffset’ Is An Undocumented Feature, This Selects The ‘bar’ Centres
ydt(k1,:) = hBar(k1).YData; % Individual Bar Heights
end
errorbar(ctr, ydt, AngleError,'.');
title('Elbow Flexion Angle for 3 Subjects with a Goal of 90 Degrees','FontSize',14, "FontWeight","bold")
xlabel('Subject','FontSize',12, "FontWeight","bold")
ylabel('Elbow Angle (deg)','FontSize',12, "FontWeight","bold")
yln = yline (90);
legend(hBar,'Baseline','Experimental', "Location","northeast")
ylim([0 130])
producing:
.
  댓글 수: 2
Jordyn Shaw
Jordyn Shaw 2021년 2월 22일
Amazing! Thank you so much!
Star Strider
Star Strider 2021년 2월 23일
As always, my pleasure!

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by