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 Center および File ExchangeErrorbars についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by