Help with Errorbars on Bar Graph

4 vues (au cours des 30 derniers jours)
Jordyn Shaw
Jordyn Shaw le 22 Fév 2021
Commenté : Star Strider le 23 Fév 2021
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 commentaire
Jordyn Shaw
Jordyn Shaw le 22 Fév 2021
This is what I get using the current code but I need to centre my individual errorbars.

Connectez-vous pour commenter.

Réponse acceptée

Star Strider
Star Strider le 22 Fév 2021
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 commentaires
Jordyn Shaw
Jordyn Shaw le 22 Fév 2021
Amazing! Thank you so much!
Star Strider
Star Strider le 23 Fév 2021
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Errorbars dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by