Adding Error Bars to a grouped Bar Plot

10 ビュー (過去 30 日間)
oliver fernihough
oliver fernihough 2021 年 4 月 20 日
回答済み: Andrew Frane 2024 年 11 月 24 日 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

回答 (2 件)

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

Andrew Frane
Andrew Frane 2024 年 11 月 24 日 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')

カテゴリ

Help Center および File ExchangeErrorbars についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by