I cannot get these error bars to work for the life of me

19 vues (au cours des 30 derniers jours)
Colton Rippey
Colton Rippey le 10 Juil 2020
Commenté : Star Strider le 9 Fév 2021
I have been trying to put in error bars for the last 3 hours to no avail. I keep getting the error codes "error using errorbar>check single input Xdata must be the same size as Ydata" and "error in errorbar. X = checksingleinput (x, sz, 'xData');.
Here is my code thus far.
x = categorical({'18-39','40-59','60+'});
y = [0.38850 .7817 0.4094 1.0100; 0.3026 0.0779 -0.1277 -.51500; -0.1316 -0.1213 -0.7811 -1.2379];
bar(x,y)
pos = [.376 .209 .709 1.15 .376 .209 .059 -.017 -.042 .029 -.524 -.381]
neg = [.226 -.041 .292 .155 .226 -.041 -.333 -.756 -.159 -.193 -1.25 -1.83]
title("Multivariate Association");
ylabel("Z-Score")
xlim({'18-39','60+'})
ylim([-1.50 1.50])
legend({'Group 1','Group 2','Group 3','Group 4'})
hold on
er = errorbar(x,y,neg,pos);
er.Color = [0 0 0];
er.LineStyle = 'none';
hold off
I am pretty new to Matlab, so please forgive my lack of understanding. I am currently using MATLAB R2020a

Réponse acceptée

Star Strider
Star Strider le 10 Juil 2020
This unfortunately will not work with categorical variables, so it is necessary to define the x-coordinates as numeric, then label the x-ticks appropriately later.
Try this:
xc = categorical({'18-39','40-59','60+'});
x = 1:numel(xc);
y = [0.38850 .7817 0.4094 1.0100; 0.3026 0.0779 -0.1277 -.51500; -0.1316 -0.1213 -0.7811 -1.2379];
hBar = bar(x,y);
pos = [.376 .209 .709 1.15 .376 .209 .059 -.017 -.042 .029 -.524 -.381];
neg = [.226 -.041 .292 .155 .226 -.041 -.333 -.756 -.159 -.193 -1.25 -1.83];
posr = reshape(pos, 4, []);
negr = reshape(neg, 4, []);
for k1 = 1:size(y,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
hold on
errorbar(ctr, ydt, posr, negr, '.r') % Plot Error Bars
hold off
set(gca, 'XTick',x, 'XTickLabel',xc)
You may need to change the sizes and orientations of ‘posr’ and ‘negr’, because it is not obvious (to me) how they should be assigned in the matrices.
  4 commentaires
Benjamin Riley
Benjamin Riley le 9 Fév 2021
Hello, this worked very well as a fix to a similar problem I had. However, I now have the error bars appearing in my legend as "data 1, data 2, data 3". Is there any way to fix this? I'm new to MATLAB, so any help is appreciated.
Star Strider
Star Strider le 9 Fév 2021
Benjamin Riley — With respect to the code I posted, adapt this legend call:
legend(hBar,'Characteristic #1','Characteristic #2','Characteristic #3','Characteristic #4')
to your plot It will then only have legend entries for the bar object and not the errorbar objects. (I tested it just now to be certain.) This approach is mentioned in the legend documentation in Included Subset of Graphics Objects in Legend .

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

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

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by