Show numbers with Grouped Bars

6 次查看(过去 30 天)
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