add standard deviation value and text on bars
조회 수: 31 (최근 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
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?
답변 (1개)
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
2020년 11월 30일
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!