Displaying a value on top of bar chart
3 views (last 30 days)
Show older comments
Bimal Ghimire
on 3 Sep 2020
Commented: Bimal Ghimire
on 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.

Accepted Answer
Cris LaPierre
on 3 Sep 2020
Edited: Cris LaPierre
on 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 Comments
More Answers (0)
See Also
Categories
Find more on Bar Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!