Adding standard error bars to grouped bar graph

4 ビュー (過去 30 日間)
Heather
Heather 2019 年 3 月 22 日
コメント済み: elena kerjean 2020 年 4 月 21 日
I was able to produce the bar graph below using the following code. After reading the doc and some other threads, I still can't seem to add standard error bars. Any advice would be appreciated!
y=[0.095 0.037 0.132; 0.1 0.058 0.158; 0 0 0.02; 0 0 0]
bar(y)
set(gca, 'XTickLabel', {'Rural' 'Exurban' 'Suburban' 'Urban'});
hold on
Bear Density Bar.jpg

採用された回答

Star Strider
Star Strider 2019 年 3 月 22 日
You are in luck in not using categorical variables, because this approach will not work with them (although I’ve not had the opportunity to read through the complete R2019a Release Notes yet, so there may be options I’m not aware of).
Try this:
y=[0.095 0.037 0.132; 0.1 0.058 0.158; 0 0 0.02; 0 0 0]
StdErr = rand(3,4)*0.01; % Create ‘Standard Error’ Matrix
figure
hBar = bar(y, 0.8); % Return ‘bar’ Handle
for k1 = 1:size(y,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, StdErr, '.r') % Plot Error Bars
set(gca, 'XTickLabel', {'Rural' 'Exurban' 'Suburban' 'Urban'});
You did not supply your ‘Standard Error’ matrix, so I created one. Note that it must be the same size as the one I created, in order for this code to work.
  3 件のコメント
Star Strider
Star Strider 2019 年 3 月 24 日
As always, my pleasure!
elena kerjean
elena kerjean 2020 年 4 月 21 日
How could we adapte you respons for different size of y (mine is (3,5) and I have trouble to adapte) ?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by