Adding Error Bars to a grouped Bar Plot

11 views (last 30 days)
oliver fernihough
oliver fernihough on 20 Apr 2021
Answered: Andrew Frane on 24 Nov 2024 at 21:28
hey,
i have tried a few of the other answers to similar problems however i come into an issue where the error bars are all drawn together.
here is my code, any advice would be helpful
i am using matlab 2018a
% my data is WUSW, the group labels are X, the errors are WerrL & WerrH
% respectively
WUSW = [0.22 0.18;0.21 0.08; 0.22 0.14; 0.25 0.13];
X = categorical({'Flat','Lotus','Lines','Sharklet'});
X = reordercats(X,{'Flat','Lotus','Lines','Sharklet'});
WerrL = [0.01 0.06 0.03 0.06; 0.03 0.04 0.04 0.02];
WerrH = [0.02 0.03 0.03 0.04; 0.03 0.03 0.03 0.06];
%here is the code i adapted from the other answers to problems
figure
hBar = bar(WUSW, 0.8); % Return ‘bar’ Handle
for k1 = 1:size(WUSW,2)
ctr(k1,:) = bsxfun(@plus, 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, WerrL, WerrH)
%when i do this all the error bars display correctly but are connected like
%a daisy chain

Answers (2)

Star Strider
Star Strider on 20 Apr 2021
Change the errorbar call slightly to:
errorbar(ctr, ydt, WerrL, WerrH, '.', 'MarkerSize',0.25)
That should do what you want.

Andrew Frane
Andrew Frane on 24 Nov 2024 at 21:28
To make error bars without a connecting line, just set the 'LineStyle' to 'none', using a name-value pair in the errorbar command:
errorbar(ctr, ydt, WerrL, WerrH, 'LineStyle', 'none')

Community Treasure Hunt

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

Start Hunting!