Show numbers with Grouped Bars
20 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Brave A
am 28 Feb. 2021
Kommentiert: Benjamin Kraus
am 1 Nov. 2024 um 0:05
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!
0 Kommentare
Akzeptierte Antwort
Star Strider
am 28 Feb. 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:
.
0 Kommentare
Weitere Antworten (1)
Benjamin Kraus
am 31 Okt. 2024 um 23:10
Bearbeitet: Benjamin Kraus
am 31 Okt. 2024 um 23:21
With the release of MATLAB R2024b, this has gotten much easier!
Starting in MATLAB R2024b, Bar objects now have a Labels property that can be used to add labels to the tops of your bars. You can read about the new properties on the Bar objects property page.
For example:
x = [98 96; 142 52; 49 49];
hBar = bar(x, FontSize=20);
hBar(1).Labels = hBar(1).YData;
hBar(2).Labels = hBar(2).YData;
grid on
set(gca, FontSize=20)
xticks(1:3)
xticklabels(["A_1", "A_2", "A_3"]);
legend(hBar, ["Baseline-1","Baseline-2"], FontSize=20);
Or, using a slightly advanced maneuver (this will produce the same result as above):
x = [98 96; 142 52; 49 49];
hBar = bar(x, FontSize=20);
set(hBar, {'Labels'}, get(hBar, 'YData'));
grid on
set(gca, FontSize=20)
xticks(1:3)
xticklabels(["A_1", "A_2", "A_3"]);
legend(hBar, ["Baseline-1","Baseline-2"], FontSize=20);
Solution for MATLAB R2019b through R2024a
If you can't upgrade to MATLAB R2024b just yet, there is still a simpler way than the solution posted above that uses the undocumented XOffset property. This solution works with MATLAB R2019b and later, and uses the (fully documented) XEndPoints and YEndPoints properties.
x = [98 96; 142 52; 49 49];
hBar = bar(x);
text([hBar.XEndPoints], [hBar.YEndPoints], string([hBar.YData]), ...
FontSize=20, VerticalAlignment='bottom', HorizontalAlignment='center')
grid on
set(gca, FontSize=20)
xticks(1:3)
xticklabels(["A_1", "A_2", "A_3"]);
legend(hBar, ["Baseline-1","Baseline-2"], FontSize=20);
2 Kommentare
Star Strider
am 31 Okt. 2024 um 23:36
I noticed tthat in the Release Notes!
The XEndPoints and YEndPoints properties still work to do something similar, and are appropriate for adding error bars to the tops of the bar bars.
Benjamin Kraus
am 1 Nov. 2024 um 0:05
@Star Strider: Indeed, the XEndPoints and YEndPoints properties are still useful for those things we don't support out-of-the-box, but hopefully we can make more features (including things like error bars on bar charts) easier to do in future releases.
Siehe auch
Kategorien
Mehr zu Whos finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!