Display values in bar plot (app designer)
조회 수: 26 (최근 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
추가 답변 (4개)
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');
omar shirko
2023년 7월 8일
%Greeting
% I encountered an issue with values displayed above the bar. To enhance accuracy, I need to reduce the font size.
% The applying code as follow
q = [0.3 0.35 0.4 0.28 0.6];
kl = 256; % key length
sm = 25; % number of simulation run
g = zeros(1,sm); % Initiallize the sum to zero
g2 = zeros(1,sm); % Initiallize the sum to zero
z = zeros(1,sm); % Initiallize the sum to zero
figure;
v1=0;
v2=0;
for j =1:sm
N = randi([1 5],1);
y = 0;
y2 = 0;
for k = 1:N
y = y+ q(k)*kl; % computing the sum
end
y2 = N * kl;
g(j) = y;
g2(j) = y2;
%z(j) = [y y2];
v1 = v1 + y;
v2 = v2 + y2;
end
hold on;
xx = 1:sm;
%g= sort(g);
%g2= sort(g2);
%z = [g, g2];
%bar(g2,0.6);
%hold on;
%bar(g,0.4);
subplot(2,2,1);
bar(xx-0.25,g2,0.4,'red');
hold on;
bar(xx+0.25,g,0.4,'blue');
axis([0 26 0 1300])
title('Sample 1');
xlabel('Simulation Number');
ylabel('Total Generated Keys (bits)');
legend('Without SDQTRF', 'With SDQTRF');
text(1:length(g2),g2,num2str(g2'),'vert','bottom','horiz','center');
text(1:length(g),g,num2str(g'),'vert','bottom','horiz','center');
box on
댓글 수: 1
Benjamin Kraus
2023년 7월 10일
You are more likely to get an answer if you post a new question to MATLAB Answers, rather than add an answer to an existing question.
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!