Show numbers with Grouped Bars

6 vues (au cours des 30 derniers jours)
Brave A
Brave A le 28 Fév 2021
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!

Réponse acceptée

Star Strider
Star Strider le 28 Fév 2021
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:
.

Plus de réponses (0)

Catégories

En savoir plus sur Animation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by