Show numbers with Grouped Bars

20 views (last 30 days)
Brave A
Brave A on 28 Feb 2021
Commented: Benjamin Kraus on 1 Nov 2024 at 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!

Accepted Answer

Star Strider
Star Strider on 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:
.

More Answers (1)

Benjamin Kraus
Benjamin Kraus on 31 Oct 2024 at 23:10
Edited: Benjamin Kraus on 31 Oct 2024 at 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 Comments
Star Strider
Star Strider on 31 Oct 2024 at 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
Benjamin Kraus on 1 Nov 2024 at 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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!