How can I add individual error bars to a grouped bar graph plot?

11 ビュー (過去 30 日間)
Emma
Emma 2024 年 8 月 27 日
編集済み: Voss 2024 年 8 月 27 日
x1 = [2.5; 7.5];
y = [y1PB y5PB; y2PB y6PB; y3PB y7PB; y4PB y8PB; y1RW y5RW; y3RW y7RW];
%error bars
errorplus = [0.000733045 0.000677482; 0.000782014 0.001159467; 0.001100796 0.000036955; 0.000698661 0.001299515; 0.000346841 0.001043998; 0.00040963 0.002191148];
errorneg = errorplus
figure;
% Plot the bar graph
b = bar(x1, y, 'FaceColor', 'flat');
[nGroups,nBars] = size(y);
hold on
x = reshape([b.XEndPoints],[nGroups, nBars]);
errorbar(x,y,errorplus, errorneg,'k','linestyle','none');
hold off

採用された回答

Voss
Voss 2024 年 8 月 27 日
Replace
x = reshape([b.XEndPoints],[nGroups, nBars]);
with
x = vertcat(b.XEndPoints);
Example, using random data:
x1 = [2.5; 7.5];
% y = [y1PB y5PB; y2PB y6PB; y3PB y7PB; y4PB y8PB; y1RW y5RW; y3RW y7RW];
y = 0.035*rand(6,2);
%error bars
errorplus = [0.000733045 0.000677482; 0.000782014 0.001159467; 0.001100796 0.000036955; 0.000698661 0.001299515; 0.000346841 0.001043998; 0.00040963 0.002191148];
errorneg = errorplus;
figure;
% Plot the bar graph
b = bar(x1, y, 'FaceColor', 'flat');
[nGroups,nBars] = size(y);
hold on
x = vertcat(b.XEndPoints);
errorbar(x,y,errorplus, errorneg,'k','linestyle','none');
hold off
  2 件のコメント
Emma
Emma 2024 年 8 月 27 日
Thank you very much, this was extremely helpful!
Voss
Voss 2024 年 8 月 27 日
編集済み: Voss 2024 年 8 月 27 日
You're welcome!
By way of explanation:
b = struct('XEndPoints',{[1 2],[3 4],[5 6],[7 8],[9 10],[11 12]});
b.XEndPoints
ans = 1x2
1 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 1x2
3 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 1x2
5 6
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 1x2
7 8
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 1x2
9 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 1x2
11 12
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
x = reshape([b.XEndPoints],[6, 2])
x = 6x2
1 7 2 8 3 9 4 10 5 11 6 12
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
x = vertcat(b.XEndPoints)
x = 6x2
1 2 3 4 5 6 7 8 9 10 11 12
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDiscrete Data Plots についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by