Clear Filters
Clear Filters

How to add error bar in Barchart Matlab

2 views (last 30 days)
Dear All,
I am trying to plot barcharts with error bars in Matlab using the code below. However, I cannot mix two groups of data together. The plot which I want should be like the first attached plot (with errorbars).
However the thing that I am getting is like this one.
In the second graph, the graphs are being separated into two groups. I am using the code below:
% data
model_series = [5815 2178 4898 1265 773 833 232 2190 189; 6246 0 3540 1164 421 0 90 672 189];
model_error = [872 326 735 189 116 124 35 329 28; 936 0 531 175 63 0 14 101 20];
b = bar(model_series, 'grouped');
hold on
[ngroups,nbars] = size(model_series);
x = nan(nbars, ngroups);
for i = 1:nbars
x(i,:) = b(i).XEndPoints;
end
errorbar(x',model_series,model_error,'k','linestyle','none');
hold off
I would be very thankful, if any could help me with this problem.

Accepted Answer

Voss
Voss on 4 Jul 2023
% data
model_series = [5815 2178 4898 1265 773 833 232 2190 189; 6246 0 3540 1164 421 0 90 672 189];
model_error = [872 326 735 189 116 124 35 329 28; 936 0 531 175 63 0 14 101 20];
b = bar(model_series.', 'grouped');
hold on
[ngroups,nbars] = size(model_series);
x = nan(nbars, ngroups);
for i = 1:ngroups
x(:,i) = b(i).XEndPoints;
end
errorbar(x,model_series.',model_error.','k','linestyle','none');
hold off

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!