add standard deviation value and text on bars

11 ビュー (過去 30 日間)
talayeh ghodsi
talayeh ghodsi 2020 年 11 月 28 日
コメント済み: Star Strider 2020 年 11 月 30 日
hi every body
I have three tipe of values and i have plot the bar of mean values. now i wnat to show the value of standard deviation to it (as in https://www.mathworks.com/help/matlab/creating_plots/bar-chart-with-error-bars.html) and also add the value text to the mean on the top of bars
Y=[0.9 0.83 0.700;1.586 1.604 1.989] %% mean values
STD_low=[0.0569,0.05499,0.0438;0.27 0.058 0.164] %% standard deviations
STD_high=STD_low;
b=bar(X,Y)
xtips1 = b(1).XEndPoints;
ytips1 = b(1).YEndPoints;
labels1 = string(b(1).YData);
text(xtips1,ytips1,labels1,'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
xtips2 = b(2).XEndPoints;
ytips2 = b(2).YEndPoints;
labels2 = string(b(2).YData);
text(xtips2,ytips2,labels2,'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
xticklabels(X)
could anyone help me with that please?
  2 件のコメント
Rik
Rik 2020 年 11 月 28 日
You seem to already know how to use the relevant functions. Your code is not formatted properly, so it is a bit harder to read than it needs to be. It looks like you are attempting to use numbered variables to process every bar. Why not use a loop?
talayeh ghodsi
talayeh ghodsi 2020 年 11 月 28 日
as my image attached, i need to add the Standard deviation to the bars
I dont know how to use loop to solve the problem
could you please help me with that?

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

回答 (1 件)

Star Strider
Star Strider 2020 年 11 月 28 日
編集済み: Star Strider 2020 年 11 月 28 日
When I try to run your code, I get:
Unrecognized function or variable 'X'.
In its absence, try this:
Y=[0.9 0.83 0.700;1.586 1.604 1.989]; %% mean values
X = 1:size(Y,2); % Substitute For Missing 'X’
STD_low=[0.0569,0.05499,0.0438;0.27 0.058 0.164]; %% standard deviations
STD_high=STD_low;
figure
b=bar(X,Y); % Return ‘bar’ Handle
xtips1 = b(1).XEndPoints;
ytips1 = b(1).YEndPoints;
for k1 = 1:size(Y,1)
ctr(k1,:) = bsxfun(@plus, b(k1).XData, b(k1).XOffset'); % Note: ‘XOffset’ Is An Undocumented Feature, This Selects The ‘bar’ Centres
ydt(k1,:) = b(k1).YData; % Individual Bar Heights
end
hold on
errorbar(ctr, ydt, STD_low, '|g') % Plot Error Bars (Note: ‘|’ Is New With R21020b, Use ‘.’ Otherwise)
labels1 = string(b(1).YData);
text(xtips1,ytips1,labels1,'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
xtips2 = b(2).XEndPoints;
ytips2 = b(2).YEndPoints;
labels2 = string(b(2).YData);
text(xtips2,ytips2,labels2,'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
xticklabels(X)
EDIT — (28 Nov 2020 at 15:24)
Added plot figure:
.
  3 件のコメント
Star Strider
Star Strider 2020 年 11 月 30 日
talayeh ghodsi wrote —
really thanks for your answer. it works great
Star Strider
Star Strider 2020 年 11 月 30 日
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by