putting error bars on bar plot?

188 ビュー (過去 30 日間)
nines
nines 2021 年 11 月 9 日
コメント済み: Star Strider 2021 年 11 月 9 日
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?

採用された回答

Star Strider
Star Strider 2021 年 11 月 9 日
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 件のコメント
nines
nines 2021 年 11 月 9 日
thank you!
Star Strider
Star Strider 2021 年 11 月 9 日
As always, my pleasure!
.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by