How to label a pie diagram

11 views (last 30 days)
Brian
Brian on 1 Sep 2013
Hi everyone, I have the following code: where I'm loading data from a spreadsheet, and outputting the data, the plot works fine but I can't figur out how to label a pie chart
a= sum (A(1:10,4)) b= sum (A(10:20,4)) c= sum (A(20:30,4)) d= sum (A(30:40,4)) e= sum (A(40:50,4)) f= sum (A(50:60,4)) g= sum (A(60:70,4)) h= sum (A(70:80,4)) i= sum (A(80:90,4)) j= sum (A(90:100,4)) k= sum (A(100:110,4)) l= sum (A(110:113,4))
y=[a,b,c,d,e,f,g,h,i,j,k,l]
x=0:10:112
subplot(2,2,1) bar(x,y),grid,xlabel('ages'),ylabel('number of people') title('number of people for ages') pause(1)
subplot(2,2,2) z= pie(y);
aa= sum (A(1:10,3)) bb= sum (A(10:20,3)) cc= sum (A(20:30,3)) dd= sum (A(30:40,3)) ee= sum (A(40:50,3)) ff= sum (A(50:60,3)) gg= sum (A(60:70,3)) hh= sum (A(70:80,3)) ii= sum (A(80:90,3)) jj= sum (A(90:100,3)) kk= sum (A(100:110,3)) ll= sum (A(110:113,3))
yy=[aa,bb,cc,dd,ee,ff,gg,hh,ii,jj,kk,ll]
pause(1) subplot(2,2,3) pie(yy)
a1= sum (A(1:10,2)) b1= sum (A(10:20,2)) c1= sum (A(20:30,2)) d1= sum (A(30:40,2)) e1= sum (A(40:50,2)) f1= sum (A(50:60,2)) g1= sum (A(60:70,2)) h1= sum (A(70:80,2)) i1= sum (A(80:90,2)) j1= sum (A(90:100,2)) k1= sum (A(100:110,2)) l1= sum (A(110:113,2))
pause(1)
yyy=[a1,b1,c1,d1,e1,f1,g1,h1,i1,j1,k1,l1] subplot(2,2,4) pie(yyy)
I found this: which says how to label a pie chart but I'm unsure how to use it.
Thanks for any help.
Labeling the Pie Chart The pie chart's labels are text graphics objects. To modify the text strings and their positions, first get the objects' strings and extents. Braces around a property name ensure that get outputs a cell array, which is important when working with multiple objects:
textObjs = findobj(h,'Type','text'); oldStr = get(textObjs,{'String'}); val = get(textObjs,{'Extent'}); oldExt = cat(1,val{:}); Create the new strings, and set the text objects' String properties to the new strings:
Names = {'Product X: ';'Product Y: ';'Product Z: '}; newStr = strcat(Names,oldStr); set(textObjs,{'String'},newStr) Find the difference between the widths of the new and old text strings and change the values of the Position properties:
val1 = get(textObjs, {'Extent'}); newExt = cat(1, val1{:}); offset = sign(oldExt(:,1)).*(newExt(:,3)-oldExt(:,3))/2; pos = get(textObjs, {'Position'}); textPos = cat(1, pos{:}); textPos(:,1) = textPos(:,1)+offset; set(textObjs,{'Position'},num2cell(textPos,[3,2]))

Accepted Answer

the cyclist
the cyclist on 1 Sep 2013
figure
pie([33 33 34],{'Pie','suck','charts'})
  2 Comments
Brian
Brian on 1 Sep 2013
thanks so much, am I missing something here??? I couldn't find it in the help section, I don't want to ask any more stupid questions than is strictly necessary....
the cyclist
the cyclist on 1 Sep 2013
This syntax is listed in both
doc pie
and
help pie
"pie(...,LABELS) is used to label each pie slice with cell array LABELS."

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!