How to Plot Numbers on top of Bar graphs?

조회 수: 37 (최근 30일)
AZ Sajjad
AZ Sajjad 2022년 10월 22일
편집: Musalula Sinkala 2022년 12월 20일
clc;
close all;
clear;
components = {'First Design', 'Second Design', 'Third Design'};
x = [66.5 33.5; 68.7 31.3; 64.9 35.1];
y = bar(x,'grouped');
xticklabels(components);
grid on
xlabel ('Design Name','fontweight','bold','FontSize',12);
ylabel ('Penetration Level (%)','fontweight','bold','FontSize',12);
legend('DG', 'PV');

채택된 답변

Star Strider
Star Strider 2022년 10월 22일
Try this —
components = {'First Design', 'Second Design', 'Third Design'};
x = [66.5 33.5; 68.7 31.3; 64.9 35.1];
figure
y = bar(x,'grouped');
xtips1 = y(1).XEndPoints;
ytips1 = y(1).YEndPoints;
labels1 = string(y(1).YData);
text(xtips1,ytips1,labels1,'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
xtips2 = y(2).XEndPoints;
ytips2 = y(2).YEndPoints;
labels2 = string(y(2).YData);
text(xtips2,ytips2,labels2,'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
xticklabels(components);
grid on
xlabel ('Design Name','fontweight','bold','FontSize',12);
ylabel ('Penetration Level (%)','fontweight','bold','FontSize',12);
legend('DG', 'PV');
ylim([min(ylim) 75])
This should work in most recent releases. If it does not work in your release (since you did not specify that, I have no idea what it is), there are other options that will work. For the present, see the bar documentation section on Specify Labels at the Ends of Bars for details.
.
  댓글 수: 5
AZ Sajjad
AZ Sajjad 2022년 10월 23일
A lot of thanks, sir,
I'm so much grateful to you. :)
Star Strider
Star Strider 2022년 10월 23일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

MarKf
MarKf 2022년 10월 22일
편집: MarKf 2022년 10월 22일
Something like this?
Edit: ah, too slow. Though this very basic approach might work with earlier releases too. You could get() the x coords for the text from the bar handle too
components = {'First Design', 'Second Design', 'Third Design'};
x = [66.5 33.5; 68.7 31.3; 64.9 35.1];
ybar= bar(x,'grouped');
xticklabels(components);
grid on
xlabel ('Design Name','fontweight','bold','FontSize',12);
ylabel ('Penetration Level (%)','fontweight','bold','FontSize',12);
legend('DG', 'PV');
[r,c]=size(x);
ybuff=2;
for ri = 1:r
for ci = 1:c
text(ri+(-0.15+0.3*(ci-1)),x(ri,ci)+ybuff,num2str(x(ri,ci),'%0.0f%%'),...
'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
end
end
ylim([0 100])
  댓글 수: 1
AZ Sajjad
AZ Sajjad 2022년 10월 22일
Sir, your graph is so much beautiful. In particular, you mentioned the Percentage Unit. I really appreciate it.
By the way, sir
I needed a favor from you.
As in your previous graph, how can I mention the M$ Unit in this graph?
I have given the code below for ease of understanding.
clc;
close all;
clear;
figure
components = {'First Design', 'Second Design', 'Third Design'};
y = [4.735, 5.230, 4.568];
hB = bar(y,'FaceColor','flat');
xticklabels(components);
C = colororder; % retrieve default colororder vector
hB.CData = C(1:numel(y),:); % use first N
grid on
xlabel ('Design Name','fontweight','bold','FontSize',12);
ylabel ('Total Net Present Cost (M$)','fontweight','bold','FontSize',12);
text(1:length(y), y', num2str(y','%0.2f'),'HorizontalAlignment','center','VerticalAlignment','bottom')
hAx = gca; % get current axes handle
hAx.YAxis.TickLabelFormat = '%0.1f'; % fix up the funky numeric display
% now add a legend by faking another plot that will create the handles
hold on
hA = area(nan(numel(components))); % area will be patch
set(hA,{'FaceColor'},mat2cell(hB.CData,[ones(size(y))],3)); % set areas to match bar face colors
hLg = legend(hA,components);

댓글을 달려면 로그인하십시오.


Musalula Sinkala
Musalula Sinkala 2022년 12월 20일
편집: Musalula Sinkala 2022년 12월 20일
A simple loop will do
figure()
bv = bar(x,'grouped');
% add the numbers
for ii = 1:numel(bv)
% the positions
xLoc = bv(ii).XEndPoints;
yLoc = bv(ii).YEndPoints;
% labels with %
theLabels = strcat( string(yLoc),'%') ;
% a vectorised one liner
text(xLoc, yLoc, theLabels, 'vert','bottom','horiz','center');
end

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by