Display values in bar plot (app designer)
조회 수: 85(최근 30일)
표시 이전 댓글
I have a problem with the code,
Why do I not see values in a graph like in Figure
Notice the yellow in the picture
X = categorical({'Small','Medium','Large'});
X = reordercats(X,{'Small','Medium','Large'});
Y = [2 3 6];
bar(X,Y);
b = bar(app.UIAxes,X,Y);
for i=1:1:length(X)
text(i:i,Y(i)',num2str(Y(i)','%0.2f'),'HorizontalAlignment','center','VerticalAlignment','bottom')
box off
end

댓글 수: 0
채택된 답변
Ameer Hamza
2020년 9월 30일
Pass app.UIAxes as first argument of text()
text(app.UIAxes, i:i, Y(i)', num2str(Y(i)','%0.2f'),'HorizontalAlignment','center','VerticalAlignment','bottom')
댓글 수: 0
추가 답변(3개)
Shahar ben ezra
2020년 9월 30일
댓글 수: 1
Ameer Hamza
2020년 10월 1일
In app designer, pass the handle of the UIAxes object to all graphical functions. Otherwise a window will pop-up
box(app.UIAxes, "off")
Sara Macchiavello
2020년 11월 28일
Hi!
I'd like to know how can I set the values inside the bar plot, just like this:

I obtain the following bar plot using the code above but I can't place values in the correct center position.

b = bar(X,Y);
hold on
b.FaceColor = 'flat';
b.CData(1,:) = [0 0.447 0.741]; % color editing
b.CData(2,:) = [0.850 0.325 0.098];
b.CData(3,:) = [0.466 0.674 0.188];
b.FaceAlpha = .7; % opacity
text(1:length(Y),Y,num2str(Y'),'vert','bottom','horiz','center'); % values position
std = [2.1 4.4 6]; % standard deviation
er = errorbar(X,Y,std);
er.Color = [0 0 0];
er.LineStyle = 'none';
set(gca, 'ytick', []);
box off
Can you help me, please?
Thank you,
Sara
댓글 수: 1
Benjamin Kraus
2020년 12월 2일
Sara,
You are more likely to get an answer if you post a new question to MATLAB Answers, rather than add to an existing question.
As long as you are not using a stacked bar plot, I think all you need to do in your case is divide Y by two and switch the vertical alignment from bottom to middle.
text(1:length(Y),Y/2,num2str(Y'),'vert','middle','horiz','center');
참고 항목
범주
Find more on Develop Apps Using App Designer in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!