Adding Error Bars to a grouped Bar Plot

6 vues (au cours des 30 derniers jours)
oliver fernihough
oliver fernihough le 20 Avr 2021
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

Réponses (2)

Star Strider
Star Strider le 20 Avr 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 le 24 Nov 2024
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')

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by