add standard deviation value and text on bars

33 views (last 30 days)
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 Comments
Rik
Rik on 28 Nov 2020
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 on 28 Nov 2020
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?

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 28 Nov 2020
Edited: Star Strider on 28 Nov 2020
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 Comments
Star Strider
Star Strider on 30 Nov 2020
talayeh ghodsi wrote —
really thanks for your answer. it works great
Star Strider
Star Strider on 30 Nov 2020
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!