Error bars on grouped bar plots

13 vues (au cours des 30 derniers jours)
Juliana Fernandez
Juliana Fernandez le 9 Juin 2020
Commenté : Star Strider le 9 Juin 2020
Hi, I have this data:
PPdata = [960.6 960.3 960.4; 960.5 960.7 960.7];
PPerr = [0.4 0.4 0.5; 0.1 0.1 0.1];
PPx=categorical({'\it CL','\it DM'});
And I have to create a bar plot with error bars, I am using this code and it works for creating the bar plot
figure
bar(PPx,PPdata)
hold on
title('Peak position by region')
ylim([960.2 961.2]);
legend({'Cervical','Cuspal','Intercuspal'},'Location','southoutside')
But when I want to add the error bars (PPerr = [0.4 0.4 0.5; 0.1 0.1 0.1]), using:
errorbar(PPx,PPdata,PPerr);
this happens:
Error using errorbar>checkSingleInput (line 270)
XData must be the same size as YData.
And when I use:
errorbar(PPdata,PPerr);
This is the plot that appears:
What can I do?

Réponse acceptée

Star Strider
Star Strider le 9 Juin 2020
Try this:
PPdata = [960.6 960.3 960.4; 960.5 960.7 960.7];
PPerr = [0.4 0.4 0.5; 0.1 0.1 0.1];
% PPx=categorical({'\it CL','\it DM'});
figure
hBar = bar(PPdata);
hBar(1).XData
hold on
for k1 = 1:size(PPdata,2)
ctr(k1,:) = bsxfun(@plus, 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
hold on
errorbar(ctr, ydt, PPerr.', '.r')
PPx=categorical({'\it CL','\it DM'});
set(gca, 'XTickLabel', PPx)
title('Peak position by region')
ylim([960.2 961.2]);
legend({'Cervical','Cuspal','Intercuspal'},'Location','southoutside')
producing:
  2 commentaires
Juliana Fernandez
Juliana Fernandez le 9 Juin 2020
Hey! Thank you very much! It is just what I was looking for.
Star Strider
Star Strider le 9 Juin 2020
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Distribution Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by