Hi! I am trying to add the error bars in my chart but I do not know how to do it...
>> y = [9862.5 5238 3845.2; 9368.6 3515 3625.7; 11064 6810 4073.6; 12599.2 5701 3955.3; 10114.4 5683 3664.5];
>> b = bar(y,'FaceColor', 'flat');
>> set (gca, 'XTickLabel',{ 'Pronghorn', 'Settler CL', 'Robidoux', 'LCS Chrome', 'Warhorse'})
>> for k = 1:size(y,2)
b(k).CData = k;
end;
this is the code for my chart.
And these are the std error:
1699.5 , 612.157 , 548.048
Thank you!

2 comentarios

I tried one of the codes but that is what happen:
>> count = [9862.5 5238 3845.2; 9368.6 3515 3625.7; 11064 6810 4073.6; 12599.2 5701 3955.3; 10114.4 5683 3664.5];
errLim = [1699.5 612.157 548.048];
err = errLim - count;
figure;
bar(count)
hold on
errorbar(1:length(count), [9862.5 5238 3845.2; 9368.6 3515 3625.7; 11064 6810 4073.6; 12599.2 5701 3955.3; 10114.4 5683 3664.5], err(1,:), err(2,:), 'LineStyle','none')
set(gca, 'xtick', 1:length(count), 'xticklabel', {'Pronghorn', 'Settler CL', 'Robidoux', 'LCS Chrome', 'Warhorse'});
Error using errorbar>checkSingleInput (line 270)
XData must be the same size as YData.
Error in errorbar (line 94)
x = checkSingleInput(x, sz, 'XData');
>> end;

Iniciar sesión para comentar.

 Respuesta aceptada

Adam Danz
Adam Danz el 14 de En. de 2020
Editada: Adam Danz el 10 de Dic. de 2021

1 voto

Here's a demo you can follow to add error bars to a grouped bar chart.
To center the errorbar on each bar, you need to compute their center points along the x axis.
In Matlab R2019B or later the bar center points are stored in
h = bar(__);
h.XEndPoints % x centers
Prior to Matlab R2019B, use an undocumented property XOffset explained here.
Required inputs are
  • count: an n x m matrix that will produce n groups each containing m bars.
  • err: an n x m matrix that defines the error for each bar
% Create bar data
count = [9862.5 5238 3845.2; 9368.6 3515 3625.7; 11064 6810 4073.6; 12599.2 5701 3955.3; 10114.4 5683 3664.5];
% Create error data (using random data for this demo)
err = (rand(size(count))+2)*100;
% Plot bars
figure;
h = bar(count);
% Get x centers; XOffset is undocumented
xCnt = (get(h(1),'XData') + cell2mat(get(h,'XOffset'))).';
% Add errorbars
hold on
errorbar(xCnt(:), count(:), err(:), err(:), 'k', 'LineStyle','none')
% or
% errorbar(xCnt(:), count(:), err(:), 'LineStyle','none')
Alternatively, if you're working with error intervals that define the upper and lower bounds of error,
Required inputs are
  • count: an n x m matrix that will produce n groups each containing m bars.
  • errUpperBound: an n x m matrix that defines the upper bound of error for each bar
  • errLowerBound: an n x m matrix that defines the lower bound of error for each bar
% Create bar data
count = [9862.5 5238 3845.2; 9368.6 3515 3625.7; 11064 6810 4073.6; 12599.2 5701 3955.3; 10114.4 5683 3664.5];
% Create upper and lower error bounds
errUpperBound = count + 200;
errLowerBound = count - 100;
% Compute the error distance in the neg & pos directions
errNeg = count - errLowerBound;
errPos = errUpperBound - count;
% plot error bar
errorbar(xCnt(:), count(:), errNeg(:), errPos(:), 'k', 'LineStyle','none')

4 comentarios

Luana Machado Simao
Luana Machado Simao el 14 de En. de 2020
I am having some issues in adding my std errors in this section:
% Create error data (using random data for this demo)
err = (rand(size(count))+2)*100;
Thank you so much for the help!
Adam Danz
Adam Danz el 14 de En. de 2020
I don't know what problems you're having unless you tell me.
In your question, you only list 3 error values. It's not clear how 3 error values should be applied to 15 bars.
Luana Machado Simao
Luana Machado Simao el 14 de En. de 2020
Sorry I didn't see my mistake there. So I have the 15 bars:
count = [9862.5 5238 3845.2; 9368.6 3515 3625.7; 11064 6810 4073.6; 12599.2 5701 3955.3; 10114.4 5683 3664.5];
So, for example, for "9862.5", the error range is: 6240.0 to 13485.0
for "5238": 3933.2 to 6542.8
and so on...
how can I add this values for each bar in this line?
err = (rand(size(count))+2)*100;
thank you! :)
Adam Danz
Adam Danz el 14 de En. de 2020
See the bottom of my updated answer.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 14 de En. de 2020

Editada:

el 10 de Dic. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by