Help with Errorbars on Bar Graph

4 次查看(过去 30 天)
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:
.

更多回答(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