Stacked bar chart and writing value inside each bar and a value in top of the bar
14 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Bimal Ghimire
am 1 Sep. 2020
Kommentiert: Bimal Ghimire
am 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 Kommentare
Akzeptierte Antwort
dpb
am 3 Sep. 2020
Bearbeitet: dpb
am 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 Kommentare
dpb
am 6 Sep. 2020
Bearbeitet: dpb
am 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! :)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Bar Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!