Help with Errorbars on Bar Graph

5 visualizaciones (últimos 30 días)
Jordyn Shaw
Jordyn Shaw el 22 de Feb. de 2021
Comentada: Star Strider el 23 de Feb. de 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 comentario
Jordyn Shaw
Jordyn Shaw el 22 de Feb. de 2021
This is what I get using the current code but I need to centre my individual errorbars.

Iniciar sesión para comentar.

Respuesta aceptada

Star Strider
Star Strider el 22 de Feb. de 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 comentarios
Jordyn Shaw
Jordyn Shaw el 22 de Feb. de 2021
Amazing! Thank you so much!
Star Strider
Star Strider el 23 de Feb. de 2021
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Errorbars en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by