Display values in bar plot (app designer)
176 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Ameer Hamza
on 30 Sep 2020
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 Comments
More Answers (3)
Shahar ben ezra
on 30 Sep 2020
1 Comment
Ameer Hamza
on 1 Oct 2020
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
on 28 Nov 2020
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 Comment
Benjamin Kraus
on 2 Dec 2020
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');
See Also
Categories
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!