MATLAB Answers

How can I have text labels inside a pie and percentages outside the pie chart?

84 views (last 30 days)
n on 24 Feb 2011
How can I have text labels inside a pie and percentages outside the pie chart?
thanks in advance for any tips


Sign in to comment.

Accepted Answer

Paulo Silva
Paulo Silva on 24 Feb 2011
Here's a sneaky way to have what you want, at least with version 2008b there's the only way.
edit pie
go to line 91 of the pie.m
replace the line with the code inside=0; by this
global inside %so we can access the variable outside the function
try %this is needed to keep the function working just like before
inside==0; %without giving errors when there's no value assigned for
catch %the variable inside, the default value is zero just like before
Save pie.m
Now to try it do this example
global inside %access the same variable as the one of the function
inside=0; %make it 0 to draw the percentages outside
x = [1 3 0.5 2.5 2];
explode = [0 1 0 0 0];
%labels={'1','2','3','4','5'}; %in case you want to override auto %
%pie(x,explode,labels) %use your own percentage labels
pie(x,explode); % draws the pie with auto percentage labels
hold on
inside=1; %now make it 1 so we can put labels inside
colormap jet


n on 24 Feb 2011
But for your information, when running it on Matlab 2010a, it gives an error on : mcoderegister in :
% Register handles with m-code generator
if ~isempty(h)
% mcoderegister('Handles',h,'Target',h(1),'Name','pie');
I just made that a comment for now and it works.
Paulo Silva
Paulo Silva on 24 Feb 2011
You can also add FontColor and FontSize to the text(... in pie.m and make their values global (the same way as we did with the inside value) so you can have more control on the text, there's also the distance from the centre of the pie.
The only problem I see with pie is that the text from the previous patch gets under the next patch, that's probably the reason they didn't allow more control over the pie text yet.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!