Show numbers with Grouped Bars

조회 수: 5 (최근 30일)
Brave A
Brave A 2021년 2월 28일
답변: Star Strider 2021년 2월 28일
I have this code and I want to improve it to make clear. Firstly, I want to show the numbers above each bar. Then show the name for eaach group under them like what I drawed A1 A2 A3.
x = [98 96; 142 52; 42 42];
bar(x)
TextFontSize=20;
LegendFontSize = 18;
grid on;
legend('Baseline-1','Baseline-2');
thanks in advance!

채택된 답변

Star Strider
Star Strider 2021년 2월 28일
Try this:
x = [98 96; 142 52; 42 42];
hBar = bar(x);
for k1 = 1:size(x,2)
ctr(k1,:) = bsxfun(@plus, 1:numel(hBar(k1).XData), hBar(k1).XOffset'); % Note: ‘XOffset’ Is An Undocumented Feature, This Selects The ‘bar’ Centres
ydt(k1,:) = hBar(k1).YData; % Individual Bar Heights
text(ctr(k1,:),ydt(k1,:),compose('%d',ydt(k1,:)), 'HorizontalAlignment','center', 'VerticalAlignment','bottom')
end
TextFontSize=20;
LegendFontSize = 18;
grid on;
set(gca,'XTickLabel',{'A_1','A_2','A_3'});
legend('Baseline-1','Baseline-2');
producing:
.

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by