How to assign error bars to a multiple data sets histogram ?

I want to compared to conditions, with 6 values in each. I have this for th moment (fig.1) :
Sans=[21, 4, 4, 18, 7, 6];
Avec=[18, 3, 4, 14, 7, 27];
Comparaison=[Sans',Avec']
figure
bar(1:6,Comparaison)
legend('Sans','Avec');
When I use only one data set I manage to assign error bars, like this (fig.2):
Ect_type_sans=[7, 4, 3, 9, 7, 8];
bar(Sans)
hold on
errorbar(1:6,Sans,Ect_type_sans,'ko')
I would like to assign error bars to the first histogram, with Ect_type_sans for the Sans data and Ect_type_avec=[6, 3, 2, 5, 6, 8] for the Avec data. Any ideas ?

 採用された回答

Star Strider
Star Strider 2021 年 7 月 18 日

1 投票

Try this —
Sans=[21, 4, 4, 18, 7, 6];
Avec=[18, 3, 4, 14, 7, 27];
Comparaison=[Sans; Avec];
Ect_type_sans=[7, 4, 3, 9, 7, 8];
Ect_type_avec=[6, 3, 2, 5, 6, 8];
Ect_type = [Ect_type_sans; Ect_type_avec];
figure
hBar = bar(1:6,Comparaison); % Return ‘bar’ Handle
for k1 = 1:size(Comparaison,1)
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, Ect_type, '.g', 'MarkerSize',1)
legend('Sans','Avec', 'Location','best');
I made a few changes that do not significantly affect the code or the plot, however they make the error bar plotting easier. I specified the error bars as green. They can be any colour you want.
.

4 件のコメント

ThomAlb
ThomAlb 2021 年 7 月 29 日
Is there any way to only show the positive side of the standard deviation?
Thank you in advance!
Yes.
Try this —
Sans=[21, 4, 4, 18, 7, 6];
Avec=[18, 3, 4, 14, 7, 27];
Comparaison=[Sans; Avec];
Ect_type_sans=[7, 4, 3, 9, 7, 8];
Ect_type_avec=[6, 3, 2, 5, 6, 8];
Ect_type = [Ect_type_sans; Ect_type_avec];
figure
hBar = bar(1:6,Comparaison); % Return ‘bar’ Handle
for k1 = 1:size(Comparaison,1)
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, [], Ect_type, '.g', 'MarkerSize',1)
legend('Sans','Avec', 'Location','N');
The errorbar function has the ability to plot different values for the upper and lower error bars, so to the plot only the positive values, define the negative values as the empty array ([]).
.
ThomAlb
ThomAlb 2021 年 7 月 30 日
Thanks a lot !!
Star Strider
Star Strider 2021 年 7 月 30 日
As always, my pleasure!
.

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

その他の回答 (2 件)

Jonas
Jonas 2021 年 7 月 18 日

1 投票

use
myBars=bar(1:6,Comparaison);
this will give you a 1x2 bar in the variable myBars in this case. One bar object holds the 'avec' data, the other the 'sans'. each bar objects carries twoporpertirs from which you can calculate the position of the bars. one property is the XEndPoints (accessed eg by mybar(1).XEndPoints) which carries the end Po nts of the bars of the their respective data set. the other property is the BarWidth property (accessed eg by mybar(1).BarWidth). from that you can calculate the center of your bars and place your errorbars like you did in the single example
ThomAlb
ThomAlb 2021 年 7 月 18 日

0 投票

Thanks a lot to both of you !!
Star Strider, your code is just perfect !

1 件のコメント

Star Strider
Star Strider 2021 年 7 月 18 日
As always, my pleasure!
Thank you!
.

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

カテゴリ

ヘルプ センター および File ExchangeErrorbars についてさらに検索

製品

リリース

R2020b

質問済み:

2021 年 7 月 18 日

コメント済み:

2021 年 7 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by