How to add numerical value in the stacked bar chart

91 ビュー (過去 30 日間)
Shariful Islam
Shariful Islam 2022 年 7 月 7 日
コメント済み: Shariful Islam 2022 年 7 月 11 日
Dear Altruist,
Here is my code. I want to add percentage vaule in the bar, like 50, 45, 5. I have attached a image. Now, how can I update my current code for this?
Regards,
Shariful
subplot(4,1,1)
y1 = [50 45 5; 36 64 0; 47 23 30; 0 52 48; 26 74 0];
x1 = [1,2,3,4,5];
bar(x1, y1,'stacked')
ylabel('Percentage')
% applyhatch(gcf,'x-+.')
subplot(4,1,2)
x2 = [6,7,8,9,10]
x2 = 1×5
6 7 8 9 10
y2 = [50 45 5; 36 64 0; 47 23 30; 0 52 48; 26 74 0];
bar(x2, y2,'stacked')
ylabel('Percentage')
% applyhatch(gcf,'x-+.')
subplot(4,1,3)
x3 = [11,12,13,14,15]
x3 = 1×5
11 12 13 14 15
y3 = [50 45 5; 36 64 0; 54 0 46; 0 52 48; 26 74 0];
bar(x3, y3,'stacked')
ylabel('Percentage')
% applyhatch(gcf,'x-+.')
subplot(4,1,4)
x4 = [16,17,18,19,20]
x4 = 1×5
16 17 18 19 20
y4 = [50 45 5; 36 64 0; 47 23 30; 0 52 48; 26 74 0];
bar(x4, y4,'stacked')
ylabel('Percentage')

採用された回答

Adam Danz
Adam Danz 2022 年 7 月 7 日
編集済み: Adam Danz 2022 年 7 月 7 日
Follow this example that uses XEndPoints and YEndPoints bar properties to compute the center of each stacked bar. The text shows the percentage of the segment within the stack.
In this example bar(x,y,'stacked'), x is a 1x5 vector and y is an nx5 matrix which will produce 5 stacks of n segments.
Update I just noticed you're using MATLAB R2015a. These bar properties were not available until later. Additionally, my example uses implicit expansion and a syntax of bar3 that was not available in 15a. If you can update MATLAB that would be best (for lots of reasons). Otherwise, you can compute the vertical centers of the bars using
ybarCnt = cumsum(y')-y'/2;
x = 1:5;
rng('default') % for reproducibility
y = rand(4,5) * 10;
h = bar(x, y,'stacked');
% Compute percentage
yp = y./sum(y) * 100;
% Compute bar segment centers
xbarCnt = vertcat(h.XEndPoints);
ybarTop = vertcat(h.YEndPoints);
ybarCnt = ybarTop - y/2;
% Create text strings
txt = compose('%.1f%%',yp);
% Add text
th = text(xbarCnt(:), ybarCnt(:), txt(:), ...
'HorizontalAlignment', 'center', ....
'VerticalAlignment', 'middle', ...
'Color', 'w',....
'FontSize', 8);
  11 件のコメント
Adam Danz
Adam Danz 2022 年 7 月 11 日
> how can I remove the text from bar graph"0.0%"?
Using the variable names from my answer, after creating the txt array, add this line to replace "0%" with empty character vectors.
txt(yp==0) = {''};
About the hatched fill function the File Exchange, sorry, I'm not familiar with that submission. You may want to ask the author or search for that function in the forum to see if other users asked about this and found a solution.
Shariful Islam
Shariful Islam 2022 年 7 月 11 日
@Adam Danz, Thanks a lot! :D

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLabels and Annotations についてさらに検索

製品


リリース

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by