什么是雷达图?雷达图也称为网络图、蜘蛛图、星图、蜘蛛网图,它被认为是一种表现多维数据的图表。它将多个维度的数据量映射到坐标轴上,每一个维度的数据都分别对应一个坐标轴,这些坐标轴以相同的间距沿着径向排列,并且刻度相同。连接各个坐标轴的网格线通常只作为辅助元素,将各个坐标轴上的数据点用线连接起来就形成了一个多边形。坐标轴、点、线、多边形共同组成了雷达图。如下图:
怎么用matlab制作多边形雷达图?
程序如下:
主函数:
clcclose allclearprefer=[4,7;20,50;85,95;75,85;1.2,2.5;5,6];limit=[2,10;10,70;70,110;65,95;0.5,4;3,9];clf;draw_radar([6,35,96,70,2,5],limit,prefer,{'维度1/s','维度2/s','维度3/℃','维度4/℃','维度5/(g/s)','维度6/%'});
2.子函数
function draw_radar(data,lim,prefer_range,labels) n=length(data); adj_data=zeros(n,1); point=zeros(n,2); adj_preferl=zeros(n,1); preferl_point=zeros(n,2); adj_preferu=zeros(n,1); preferu_point=zeros(n,2); set(gca,'units','normal','pos',[0 0 1 1]); axis off axis equal hold on theta_last=pi/2; for i=1:n theta=2*pi/n*i pi/2; plot([0,500*cos(theta)],[0,500*sin(theta)],'k-','linewidth',2); for j=1:5 plot([j*100*cos(theta_last),j*100*cos(theta)],[j*100*sin(theta_last),j*100*sin(theta)],'--','linewidth',0.75,'color',[0.5,0.5,0.5]); end theta_last=theta; if data(i)1) adj_data(i)=0; elseif data(i)>lim(i,2) adj_data(i)=500; else adj_data(i)=(data(i)-lim(i,1))/(lim(i,2)-lim(i,1))*500; end point(i,1:2)=[adj_data(i)*cos(theta);adj_data(i)*sin(theta)]; adj_preferl(i)=(prefer_range(i,1)-lim(i,1))/(lim(i,2)-lim(i,1))*500; preferl_point(i,1:2)=[adj_preferl(i)*cos(theta);adj_preferl(i)*sin(theta)]; adj_preferu(i)=(prefer_range(i,2)-lim(i,1))/(lim(i,2)-lim(i,1))*500; preferu_point(i,1:2)=[adj_preferu(i)*cos(theta);adj_preferu(i)*sin(theta)]; text_around(510*cos(theta),510*sin(theta),labels{i},theta); end plot([preferl_point(:,1);preferl_point(1,1)],[preferl_point(:,2);preferl_point(1,2)],'b-','linewidth',1.5); plot([preferu_point(:,1);preferu_point(1,1)],[preferu_point(:,2);preferu_point(1,2)],'r-','linewidth',1.5); for i=1:n theta=2*pi/n*i pi/2; for j=1:5 text_around(j*100*cos(theta),j*100*sin(theta),num2str(lim(i,1) (lim(i,2)-lim(i,1))/5*j),theta pi/2,7); end end plot([point(:,1);point(1,1)],[point(:,2);point(1,2)],'k-','linewidth',0.2); fill(point(:,1),point(:,2),[0.9 0.9 0.7]) alpha(0.5); texts=findobj(gca,'Type','Text'); minx=-300; maxx=300; miny=-300; maxy=300; for i=1:length(texts) rect=get(texts(i),'Extent'); x=rect(1); y=rect(2); dx=rect(3); dy=rect(4); if x minx=x; elseif x dx>maxx maxx=x dx; end if y miny=y; elseif y dy>maxy maxy=y dy; end end axis([minx-50,maxx 50,miny-20,maxy 20]);end
function text_around(x,y,txt,theta,fontsize) if nargin==4 fontsize=10; end section=mod(theta pi/12,2*pi); if section>pi pi/6 %上对齐 if section>1.5*pi pi/6 %左对齐 text(x,y,txt,'VerticalAlignment','cap','HorizontalAlignment','left','Fontsize',fontsize); elseif section>1.5*pi %中对齐 text(x,y,txt,'VerticalAlignment','cap','HorizontalAlignment','center','Fontsize',fontsize); else %右对齐 text(x,y,txt,'VerticalAlignment','cap','HorizontalAlignment','right','Fontsize',fontsize); end elseif section>pi %中、右对齐 text(x,y,txt,'VerticalAlignment','middle','HorizontalAlignment','right','Fontsize',fontsize); elseif section>pi/6 %下对齐 if section>0.5*pi pi/6 %右对齐 text(x,y,txt,'VerticalAlignment','bottom','HorizontalAlignment','right','Fontsize',fontsize); elseif section>0.5*pi %中对齐 text(x,y,txt,'VerticalAlignment','bottom','HorizontalAlignment','center','Fontsize',fontsize); else %左对齐 text(x,y,txt,'VerticalAlignment','bottom','HorizontalAlignment','left','Fontsize',fontsize); end else %中、左对齐 text(x,y,txt,'VerticalAlignment','middle','HorizontalAlignment','left','Fontsize',fontsize); end title('多边形雷达图');end
暂无评论