add standard deviation value and text on bars

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 个评论

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?
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 个)

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 个评论

The missing ‘X’ is the reason you are having problems using my code with it. It is not possible to use this code with categorical variables, because categorical values are not numeric, so it is not possible to calculate with them.
Change it to:
Y=[0.884 1.586;0.901 1.801;0.697 1.404]; %% mean values
X = categorical({'GT','whole V.','Proposed method'});
X = reordercats(X,{'GT','whole V.','Proposed method'});
Xv = 1:numel(X); % Use Numerical Values Of ‘X’ To Plot Error Bars
STD_low=[0.0125 0.27;0.024 0.0124 ;0.051 0.0584]; %% standard deviations
STD_high=STD_low;
figure
b=bar(Xv,Y); % Return ‘bar’ Handle
xtips1 = b(1).XEndPoints;
ytips1 = b(1).YEndPoints;
for k1 = 1:size(Y,2)
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
heb = errorbar(ctr, ydt, STD_low.', STD_high.', '.g'); % Plot Error Bars (Note: ‘|’ Is New With R21020b, Use ‘.’ Otherwise)
for k = 1:numel(heb)
heb(k).MarkerSize = 1;
end
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)
This works with your categorical ‘X’. (I made a few other improvements as well.)
talayeh ghodsi wrote —
really thanks for your answer. it works great
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Time Series Events 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by