程序练习:Matlab 饼图绘制

Matalb小练习

练习饼图绘制,目标是画出一个饼图

可以展示三个产品市场份额的饼图

代码如下

function week13 %%饼图
h0=figure('toolbar','none','position',[200 150 450 250],'name','week 13');
t=[54 21 35;
68 54 35;
45 25 12;
48 68 45;
68 54 69];
x=sum(t);
h=pie(x);
textobjs=findobj(h,'type','text');
str1=get(textobjs,{'string'});
val1=get(textobjs,{'extent'});
oldext=cat(1,val1{:});
names={'product1:';'product2:';'product3:'};
str2=strcat(names,str1);
set(textobjs,{'string'},str2)
val2=get(textobjs,{'extent'});
newext=cat(1,val2{:});
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]))

运行结果如下
程序练习:Matlab 饼图绘制_第1张图片

你可能感兴趣的:(Matlab)