Show numbers with Grouped Bars

21 visualizaciones (últimos 30 días)
Brave A
Brave A el 28 de Feb. de 2021
Comentada: Benjamin Kraus el 1 de Nov. de 2024 a las 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!

Respuesta aceptada

Star Strider
Star Strider el 28 de Feb. de 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:
.

Más respuestas (1)

Benjamin Kraus
Benjamin Kraus el 31 de Oct. de 2024 a las 23:10
Editada: Benjamin Kraus el 31 de Oct. de 2024 a las 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 comentarios
Star Strider
Star Strider el 31 de Oct. de 2024 a las 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 el 1 de Nov. de 2024 a las 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.

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by