putting error bars on bar plot?

58 views (last 30 days)
nines on 9 Nov 2021
Commented: Star Strider on 9 Nov 2021
I have a mean of subjects bar plot, specifically a horizontal bar plot (barh), and I want to add error bars to the the bar plot and plot it against categorical data. I have calculated the STD:
std = 34x1 double
mean = 34x1 double
x = 1x34 categorical
std = std(matrix, 0, 2)
barwitherr(std, 1:length(x), mean_matrix)
The plot runs, but doesn't add error bars.
I am getting the error:
Undefeined function barwitherr for input arguments of type double.
Can you help me?

Accepted Answer

Star Strider
Star Strider on 9 Nov 2021
I do not remember when ‘XEndPoints’ and ‘YEndPoints’ were introduced (and I am not able to find it in the documentation), so I included two options —
x = categorical({'a','b','c'});
y = [75.8 78.05; 81 80.30; 91 80.78];
err = rand(size(y))*10;
b = bar(x,y);
hold on
for k = 1:numel(b) % Recent MATLAB Versions
xtips = b(k).XEndPoints;
ytips = b(k).YEndPoints;
errorbar(xtips,ytips,err(:,k), '.g', 'MarkerSize',0.1)
hold off
b = bar(y);
for k = 1:numel(b) % Earlier MATLAB Versions
ctr(k,:) = bsxfun(@plus, b(k).XData, [b(k).XOffset]');
ydt(k,:) = b(k).YData;
hold on
errorbar(ctr, ydt, err.', '.g', 'MarkerSize',0.1)
hold off
This is a general solution, using single or grouped bar plots, and adapts to the size of ‘y’. Choose the approach that works, depending on the available MATLAB version/release.

More Answers (0)


Find more on Discrete Data Plots in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!