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

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

 Accepted Answer

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 Comments

Thank you so much for your help! I would have never gotten to that code on my own.
I am not sure what you mean by changing the sizes and orientations of 'posr' and "negr". Do you mean where each upper and lower limit of the error bar is assigned to? I am trying to assign an upper and lower error bar to each value of y, where the first upper and lower value in the error row vectors are assigned to the first value of y (and so on).
I ended up getting an error that wrote:
"Error using bar (line 213)
Values plotted against x-axis must be categorical values. To create categorical values, use the categorical function."
I thought I could fix it by changing:
"hBar = bar(x,y);" to "hBar = bar(xc,y);"
I ended up getting an output, but it came out odd:
Even after that I got another error that wrote:
"Error using matlab.graphics.axis.decorator.CategoricalRuler/validateTicks
Value must be a vector of increasing categorical values as ordered by the ruler Categories property.
Error in matlab.graphics.axis.decorator.CategoricalRuler/setTicksDelegate"
Once again, thank you so much for your help!
My pleasure.
My code correctly displayed the error bars I assigned. The resutl I get with the code I posted (however did not previously post the plot image because I am not certain how to format ‘posr’ and ‘negr’ correctly) is:
It is not possible to use my code with a categorical x-variable. That is the reason I used a numeric x-variable, and then used the set function to provide the appropriate x-tick labels. That’s simply the way MATLAB has chosen to implement this. (We’re just along for the ride.)
Using my code will get you the result I posted here. You will likely have to examine ‘posr’ and ‘negr’ to be certain they conform to what you want them to be.
.
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.
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 .

Sign in to comment.

More Answers (0)

Categories

Products

Community Treasure Hunt

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

Start Hunting!