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

조회 수: 7 (최근 30일)
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
Bimal Ghimire
Bimal Ghimire 2020년 9월 3일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

dpb
dpb 2020년 9월 3일
편집: dpb 2020년 9월 4일
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
dpb
dpb 2020년 9월 6일
편집: dpb 2020년 9월 6일
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 2020년 9월 7일
I am new to matlab. I did not have sufficient time to explore matlab. Thank you so much for your kind help.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by