Show numbers with Grouped Bars

3 ビュー (過去 30 日間)
Brave A
Brave A 2021 年 2 月 28 日
コメント済み: Benjamin Kraus 2024 年 11 月 1 日
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:
.

その他の回答 (1 件)

Benjamin Kraus
Benjamin Kraus 2024 年 10 月 31 日
編集済み: Benjamin Kraus 2024 年 10 月 31 日
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 件のコメント
Star Strider
Star Strider 2024 年 10 月 31 日
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 2024 年 11 月 1 日
@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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by