MATLAB Answers

Stacked bar chart and writing value inside each bar and a value in top of the bar

16 views (last 30 days)
Bimal Ghimire
Bimal Ghimire on 1 Sep 2020
Commented: Bimal Ghimire on 7 Sep 2020
Here, I want to show each y value inside the resepective stacked bar without adding cumulatively..To show the
value properly (As some values are really large and some are small) , I multiplied and devided value accordingly. Still
values are not shown properly. Width of the bar is not sufficient to show the value also. Additionally,
I want to show E{i} on top of each stacked bar. Moreover, for a particular value of i=index, I want to display bar with differnt color.
I am new to matlab. I could not do all. I would really appreciate your response.
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;
for i=1:20
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
% Compute the height of each segment and write text to plot
text(repmat(i,1,numel(bh)), y, compose('%.1f',y), 'Color', 'w', ...
'FontSize', 8, 'HorizontalAlignment', 'Center', 'VerticalAlignment', 'Top')
end

  4 Comments

Show 1 older comment
dpb
dpb on 2 Sep 2020
Actually, I forgot. Presuming you have at least R2019b (and maybe a release or two earlier, not sure when) TMW exposed the .X|YEndPosition values that are what you need...
Using the example for bar for stacked chart,
hBar=bar(y,'stacked');
hold on
arrayfun(@(i) text(hBar(i).XEndPoints,hBar(i).YEndPoints,num2str(hBar(i).YEndPoints.','%0.1f'), ...
'verticalalignment','top','horizontalalign','center'),[1:numel(hBar)])
yields
The value outside the bar can be done by using 'VerticalAlignment','bottom' and whatever value you desire to be displayed.
The problem of room is one I think can be solved by either making the axes wider or the font smaller if you insist on having that many bars on the plot--there are only so many pixels on the screen.
The only other idea is you could try to rotate the text 90 degrees so as to write vertically--but then yhou'll not have sufficient room in the smaller sections the other way, either.
Sometimes just try to cram too much into one figure...
Bimal Ghimire
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
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'),[1:numel(bh)])
end
I followed your statements but why bar garph is not showing every value of i. Apart from that, value of array E is not part
of the graph but I want to show on top of the stacked bar. How it can be done? Is there a way to adjust bar width according to the content?
Thank you once again for the earlier reply.

Sign in to comment.

Accepted Answer

dpb
dpb on 3 Sep 2020
Edited: dpb on 4 Sep 2020
numNearbyCHs=20;
A=randi([100,1000],numNearbyCHs,1);
B=randi([100,300],numNearbyCHs,1);
C=randi([2,30],numNearbyCHs,1);
D=randi([2,30],numNearbyCHs,1);
E=randi([0,2],numNearbyCHs,1);
y=[A B C D].*[1/5 1/5 5 5];
hBar=bar(y,0.95,'stacked');
arrayfun(@(i) text(hBar(i).XEndPoints,hBar(i).YEndPoints,num2str(hBar(i).YEndPoints.','%0.1f'), ...
'verticalalignment','top','horizontalalign','center','fontsize',6,'color','w'), ...
[1:numel(hBar)])
text(hBar(end).XEndPoints,hBar(end).YEndPoints,num2str(E,'%0.1f'), ...
'verticalalignment','bottom','horizontalalign','center','fontsize',6,'color','k')
xlim([0.25 20.75]), ylim([0 460])
results in
I'll let you go fix up the colors as wanted; with the default figure size even widening the barwidth to 0.95, minimizing the whitespace on the x axis and going to 6-pt font barely gets the text in the bar. Looks like could have raised ylim a little more, too.
But, the basic code to to do what asked for..."salt to suit!" :)

  3 Comments

dpb
dpb on 6 Sep 2020
Glad to help...have messed with bar a LOT over the years in forum to find workarounds -- it's much simpler now to do this since TMW finally exposed the needed properties/values. I'd like to think my ranting here about the poor user interface and missing feautres had some positive impact in getting that enhancement done! :)
Bimal Ghimire
Bimal Ghimire on 7 Sep 2020
I am new to matlab. I did not have sufficient time to explore matlab. Thank you so much for your kind help.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by