putting error bars on bar plot?
160 views (last 30 days)
Show older comments
Hello!
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:
values:
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?
0 Comments
Accepted Answer
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;
figure
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)
end
hold off
figure
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;
end
hold on
errorbar(ctr, ydt, err.', '.g', 'MarkerSize',0.1)
hold off
set(gca,'XTickLabel',x)
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.
.
2 Comments
More Answers (0)
See Also
Categories
Find more on Line 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!