How to add numerical value in the stacked bar chart

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')

 Réponse acceptée

Adam Danz
Adam Danz le 7 Juil 2022
Modifié(e) : Adam Danz le 7 Juil 2022
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 commentaires

Thanks @Adam Danz.
I am using MATLAB R2015a, when I am trying to run your code, it shows an error in line 4, "The length of X must match the number of rows of Y".
and I am updating my code as follows:
subplot(4,1,1)
y1 = [50 45 5; 36 64 0; 47 23 30; 0 52 48; 26 74 0];
x1 = 1:5;
bar(x1, y1,'stacked')
yp1 = y1./sum(y1) * 100;
xbarCnt = vertcat(h.XEndPoints);
Unable to resolve the name 'h.XEndPoints'.
ybarTop = vertcat(h.YEndPoints);
ybarCnt = ybarTop - y1/2;
txt = compose('%.0f%%',yp1);
th = text(xbarCnt(:), ybarCnt(:), txt(:), ...
'HorizontalAlignment', 'center', ....
'VerticalAlignment', 'middle', ...
'Color', 'w',....
'FontSize', 8);
ylabel('Percentage')
subplot(4,1,2)
x2 = 6:10;
y2 = [50 45 5; 36 64 0; 47 23 30; 0 52 48; 26 74 0];
bar(x2, y2,'stacked')
yp2 = y2./sum(y2) * 100;
xbarCnt = vertcat(h.XEndPoints);
ybarTop = vertcat(h.YEndPoints);
ybarCnt = ybarTop - y2/2;
txt = compose('%.0f%%',yp2);
th = text(xbarCnt(:), ybarCnt(:), txt(:), ...
'HorizontalAlignment', 'center', ....
'VerticalAlignment', 'middle', ...
'Color', 'w',....
'FontSize', 8);
ylabel('Percentage')
subplot(4,1,3)
x3 = 11:15;
y3 = [50 45 5; 36 64 0; 47 23 30; 0 52 48; 26 74 0];
bar(x3, y3,'stacked')
yp3 = y3./sum(y3) * 100;
xbarCnt = vertcat(h.XEndPoints);
ybarTop = vertcat(h.YEndPoints);
ybarCnt = ybarTop - y3/2;
txt = compose('%.0f%%',yp3);
th = text(xbarCnt(:), ybarCnt(:), txt(:), ...
'HorizontalAlignment', 'center', ....
'VerticalAlignment', 'middle', ...
'Color', 'w',....
'FontSize', 8);
ylabel('Percentage')
subplot(4,1,4)
x4 = 16:20;
y4 = [50 45 5; 36 64 0; 47 23 30; 0 52 48; 26 74 0];
bar(x4, y4,'stacked')
yp4 = y4./sum(y4) * 100;
xbarCnt = vertcat(h.XEndPoints);
ybarTop = vertcat(h.YEndPoints);
ybarCnt = ybarTop - y4/2;
txt = compose('%.0f%%',yp4);
th = text(xbarCnt(:), ybarCnt(:), txt(:), ...
'HorizontalAlignment', 'center', ....
'VerticalAlignment', 'middle', ...
'Color', 'w',....
'FontSize', 8);
ylabel('Percentage')
Perhaps you saw my answer before I had a chance to update it. See the bold update in my answer. In summary, there are multiple parts of my answer that won't work in 15a. Nevertheless, I've mentioned how you can compute the vertical center of each bar which is most of the battle. Let me know if you get stuck.
BTW, you can get around the first error just by removing x
bar(y1,'stacked')
but you'll come across another error in the next line because implicit expansion was not introduced until the following year from your release.
Shariful Islam
Shariful Islam le 7 Juil 2022
Modifié(e) : Shariful Islam le 7 Juil 2022
@Adam Danz, just see your update. To avoid the following error, only MATLab update is enough?
Adam Danz
Adam Danz le 7 Juil 2022
Modifié(e) : Adam Danz le 7 Juil 2022
Yes, the plot you see was generated in MATLAB R2022a.
Update here:
Thanks @Adam Danz. But in this section it is MATLAB R2022a. But here is also showing some error! What can I do?
subplot(4,1,1)
y1 = [50 45 5; 36 64 0; 47 23 30; 0 52 48; 26 74 0];
x1 = 1:5;
bar( y1,'stacked')
yp1 = y1./sum(y1) * 100;
xbarCnt = vertcat(h.XEndPoints);
Unable to resolve the name 'h.XEndPoints'.
ybarTop = vertcat(h.YEndPoints);
ybarCnt = ybarTop - y1/2;
txt = compose('%.1f%%',yp1);
th = text(xbarCnt(:), ybarCnt(:), txt(:), ...
'HorizontalAlignment', 'center', ....
'VerticalAlignment', 'middle', ...
'Color', 'w',....
'FontSize', 8);
ylabel('Percentage')
subplot(4,1,2)
x2 = 6:10;
y2 = [50 45 5; 36 64 0; 47 23 30; 0 52 48; 26 74 0];
bar(y2,'stacked')
yp2 = y2./sum(y2) * 100;
xbarCnt = vertcat(h.XEndPoints);
ybarTop = vertcat(h.YEndPoints);
ybarCnt = ybarTop - y2/2;
txt = compose('%.1f%%',yp2);
th = text(xbarCnt(:), ybarCnt(:), txt(:), ...
'HorizontalAlignment', 'center', ....
'VerticalAlignment', 'middle', ...
'Color', 'w',....
'FontSize', 8);
ylabel('Percentage')
subplot(4,1,3)
x3 = 11:15;
y3 = [50 45 5; 36 64 0; 47 23 30; 0 52 48; 26 74 0];
bar(y3,'stacked')
yp3 = y3./sum(y3) * 100;
xbarCnt = vertcat(h.XEndPoints);
ybarTop = vertcat(h.YEndPoints);
ybarCnt = ybarTop - y3/2;
txt = compose('%.1f%%',yp3);
th = text(xbarCnt(:), ybarCnt(:), txt(:), ...
'HorizontalAlignment', 'center', ....
'VerticalAlignment', 'middle', ...
'Color', 'w',....
'FontSize', 8);
ylabel('Percentage')
subplot(4,1,4)
x4 = 16:20;
y4 = [50 45 5; 36 64 0; 47 23 30; 0 52 48; 26 74 0];
bar(y4,'stacked')
yp4 = y4./sum(y4) * 100;
xbarCnt = vertcat(h.XEndPoints);
ybarTop = vertcat(h.YEndPoints);
ybarCnt = ybarTop - y4/2;
txt = compose('%.1f%%',yp4);
th = text(xbarCnt(:), ybarCnt(:), txt(:), ...
'HorizontalAlignment', 'center', ....
'VerticalAlignment', 'middle', ...
'Color', 'w',....
'FontSize', 8);
ylabel('Percentage')
Adam Danz
Adam Danz le 7 Juil 2022
Modifié(e) : Adam Danz le 8 Juil 2022
Your version doesn't follow critical parts of the demo in my answer.
Hint: bar(y1,'stacked') needs to have an output (h). See the 4th line of code in my answer.
Dear @Adam Danz, I am sorry. I have updated my MATLAB and run the following code:
subplot(4,1,1)
y1 = [50 45 5; 36 64 0; 47 23 30; 0 52 48; 26 74 0];
x1 = 1:5;
bar(x1, y1,'stacked')
yp1 = y1./sum(y1) * 100;
% Compute bar segment centers
xbarCnt1 = vertcat(h.XEndPoints);
Unable to resolve the name 'h.XEndPoints'.
ybarTop1 = vertcat(h.YEndPoints);
ybarCnt1 = ybarTop - y1/2;
% Create text strings
txt1 = compose('%.1f%%',yp1);
% Add text
th1 = text(xbarCnt1(:), ybarCnt1(:), txt1(:), ...
'HorizontalAlignment', 'center', ....
'VerticalAlignment', 'middle', ...
'Color', 'w',....
'FontSize', 8);
ylabel('Percentage')
subplot(4,1,2)
x2 = 6:10;
y2 = [50 45 5; 36 64 0; 47 23 30; 0 52 48; 26 74 0];
bar(x2, y2,'stacked')
yp2 = y2./sum(y2) * 100;
xbarCnt2 = vertcat(h.XEndPoints);
ybarTop2 = vertcat(h.YEndPoints);
ybarCnt2 = ybarTop - y2/2;
txt2 = compose('%.1f%%',yp2);
th2 = text(xbarCnt2(:), ybarCnt2(:), txt2(:), ...
'HorizontalAlignment', 'center', ....
'VerticalAlignment', 'middle', ...
'Color', 'w',....
'FontSize', 8);
ylabel('Percentage')
subplot(4,1,3)
x3 = 11:15;
y3 = [50 45 5; 36 64 0; 47 23 30; 0 52 48; 26 74 0];
bar(x3, y3,'stacked')
yp3 = y3./sum(y3) * 100;
xbarCnt3 = vertcat(h.XEndPoints);
ybarTop3 = vertcat(h.YEndPoints);
ybarCnt3 = ybarTop - y3/2;
txt3 = compose('%.1f%%',yp3);
th3 = text(xbarCnt3(:), ybarCnt3(:), txt3(:), ...
'HorizontalAlignment', 'center', ....
'VerticalAlignment', 'middle', ...
'Color', 'w',....
'FontSize', 8);
ylabel('Percentage')
subplot(4,1,4)
x4 = 16:20;
y4 = [50 45 5; 36 64 0; 47 23 30; 0 52 48; 26 74 0];
bar(x4, y4,'stacked')
yp4 = y4./sum(y4) * 100;
xbarCnt4 = vertcat(h.XEndPoints);
ybarTop4 = vertcat(h.YEndPoints);
ybarCnt4 = ybarTop - y4/2;
txt4 = compose('%.1f%%',yp4);
th4 = text(xbarCnt4(:), ybarCnt4(:), txt4(:), ...
'HorizontalAlignment', 'center', ....
'VerticalAlignment', 'middle', ...
'Color', 'w',....
'FontSize', 8);
ylabel('Percentage')
I already responded to that error. Compare your version to my version to see what's different. You're not returning the handle h=bar(...).
Dear @Adam Danz, I have done it. Thanks for your help. Another question, how can I remove the text from bar graph"0.0%" and can I create like second image (image 2. hatch pattern, I have used aplplyhatch , link: Hatched Fill Patterns - File Exchange - MATLAB Central (mathworks.com), but after use it, I cannot edit the title, xlabel, y label)?
Image 2 Image 1
> 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.
@Adam Danz, Thanks a lot! :D

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with MATLAB dans Centre d'aide et File Exchange

Produits

Version

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by