add standard deviation value and text on bars

47 次查看(过去 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 CenterFile Exchange 中查找有关 Discrete Data Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by