Stacked bar chart with errorbars

11 ビュー (過去 30 日間)
Iron1759
Iron1759 2021 年 9 月 8 日
コメント済み: Iron1759 2021 年 9 月 10 日
Hi,
I've tried a simple code to test the idea of error bars with stacked bar chart but somehow it goes wrong:
data_bar = [8.89862100831768 10.6124723247243;
9.48697169408196 10.9541916851987;
8.97084136825914 10.6925827879448]; % 3 groups - 2 stacks per group
err = [0.2, 0.4; 0.3, 0.1; 0.5, 0.7];
XX = categorical({'sample1' , 'sample2', 'sample3'}); % group names
b = bar(XXX,data_bar,'stacked');
[ngroups,nbars] = size(data_bar);
x = nan(nbars, ngroups);
y = nan(nbars, ngroups);
for i = 1:nbars
x(i,:) = b(i).XEndPoints;
y(i,:) = b(i).YEndPoints; % for the data values
end
errorbar(x',y',err,'k','linestyle','none','HandleVisibility','off');
legend({'FFT' , 'DCT'});
x and y represent the locations of the errorbars but somehow I get something like this:
without the errorbars it looks fine:
Thanks!

採用された回答

Adam Danz
Adam Danz 2021 年 9 月 8 日
編集済み: Adam Danz 2021 年 9 月 9 日
hold on % <--- you forgot this
errorbar(x',y',err,'k','linestyle','none','HandleVisibility','off');
Also, you can replace the for-loop with,
x = vertcat(b.XEndPoints)';
y = vertcat(b.YEndPoints)';
hold on
errorbar(x,y,err,'k','linestyle','none','HandleVisibility','off');
  1 件のコメント
Iron1759
Iron1759 2021 年 9 月 10 日
Sometimes the solution is easier than expected, the 'hold on' works. Thanks.

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

その他の回答 (0 件)

カテゴリ

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