Displaying a value on top of bar chart
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Bimal Ghimire
le 3 Sep 2020
Commenté : Bimal Ghimire
le 3 Sep 2020
for i=1:20
A{i}=randi([100,1000]);
B{i}=randi([100,300]);
C{i}=randi([2,30]);
D{i}=randi([2,30]);
E{i}=randi([0,2]);
end
hold on;
index=10;
numNearbyCHs=20;
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
for i=1:numNearbyCHs
y=[A{i}/5,B{i}/5,C{i}*5,D{i}*5];
bh = bar(i,y,'stacked','FaceColor', 'Flat');
% Choose a color map (using "lines" in this example)
if i~=index
colors = mat2cell(lines(numel(bh)),ones(numel(bh),1), 3);
set(bh, {'CData'}, colors)
else
colors = mat2cell(jet(numel(bh)),ones(numel(bh),1), 3);
set(bh, {'CData'}, colors)
end
arrayfun(@(i) text(bh(i).XEndPoints,bh(i).YEndPoints,num2str(bh(i).YEndPoints.','%0.1f'), ...
'verticalalignment','top','horizontalalign','center','Color','white'),[1:numel(bh)])
end
I would like to display value of E{i} on top of each stacked bar and each value of i on X-axis. Can somebody help me on this. I am really stuck on this. I would really appriate your help.

Réponse acceptée
Cris LaPierre
le 3 Sep 2020
Modifié(e) : Cris LaPierre
le 3 Sep 2020
I'd add the following code at the bottom of your for loop
text(i,sum(y)+15,string(E(i)));

0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Bar Plots 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!