how to write the percentage into pie chart?

hi all, the attached pie chart shows the percentage of variance. I want to write the percentage inside sector and the txt definition outside. Please help me.

 Accepted Answer

L = {'A','B','C','D','E'};
X = [ 1, 3,0.5,2.5, 2];
H = pie(X);
%
T = H(strcmpi(get(H,'Type'),'text'));
P = cell2mat(get(T,'Position'));
set(T,{'Position'},num2cell(P*0.6,2))
text(P(:,1),P(:,2),L(:))
Creates this:

10 Comments

Thank you for your help, Stephen. It works but doesn't agree with the actual data! here is it
4.18885217289864
26.7356605415924
1.92721720841895
2.72413971200671
18.3084577561384
6.75871344379758
44.2124575513627
29.5231966970319
20.2626754201396
0.176357093909418
7.79200786130982
30.2901091873949
Stephen23
Stephen23 on 22 May 2016
Edited: Stephen23 on 22 May 2016
@Ms. Oceanographer: can you please explain how it "doesn't agree with the actual data". Note that you did not show me your code or your input data, so I have not way to check what you are doing. Is the order somehow incorrect? You need to explain what you expect and what is wrong with the result you are getting.
the used function is var in the statistics toolbox to calculate the percentage of variance monthly.
for example:
var_nov=var(y2a(303:332));
var_nov1=var(y1a(303:332));
var_nov2=var_nov/var_nov1;
var_Nov3= 100*(abs(1-var_nov2));
Stephen23
Stephen23 on 22 May 2016
Edited: Stephen23 on 22 May 2016
@Ms. Oceanographer: you need to explain, as I requested, what you expect and what you are getting. I cannot read your mind, so I cannot magically know what the correct output should be.
You question is not related to the statistics functions (I don't have that toolbox), so you simply need to give the data for the scatter function and show how you are calling it, and explain what you expect the output pie chart should look like.
Lilya
Lilya on 22 May 2016
Edited: Lilya on 22 May 2016
Sorry for that. The correct pie chart should contain the calculated data (i.e. Those I did), not those set by the default percentage of Matlab. I just want to write mine inside the sectors (the exact matrix).
Thank you for your attention and help again Mr. Stephen
If you do not want to use the percentages calculated by MATLAB, then you can also provide some alternative text strings yourself when you call the pie function, e.g.:
pie(X,labels)
This is explained clearly in the documentation, with examples.
I appreciate it :)
Is it possible to change the font size on the Labels? Because You changed the original texts into percentage.
I got the answer. I have to just put 'FontSize' at the end text comment.
text(P(:,1),P(:,2),L(:),'FontSize',18)
Hi, this was a really helpful answer. Thank you @Stephen Cobeldick. I am wondering if you can help me relocate some of the external text labels. You'll notice "NH4+" and "Na+" are written too close to the pie chart...
Thanks in advance!

Sign in to comment.

More Answers (0)

Tags

Asked:

on 22 May 2016

Commented:

on 31 Aug 2020

Community Treasure Hunt

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

Start Hunting!