本文根据【Matlab】绘制雷达图及Legend灵活控制_XYZ-CSDN博客_matlab画雷达图
用matlab画多组的雷达图_Jane的博客-CSDN博客_matlab雷达图
MATLAB 雷达图画图函数_mt96mt96mt的博客-CSDN博客_matlab 雷达图修改,对原文作者表示感谢。
雷达图如下图所示:
我做的改变是在原有基础上加了显示图例。
修改的原理是:原代码96行为颜色进行填充,那么图例其实就是显示这些颜色,借鉴CallMeJacky的思路,设置函数句柄H,最后显示这些图例即可。
代码如下:
function draw_radar2(alldata,lim,labels)
% draw radial map
% INPUT
% alldata: Nvar * Ngroup
% lim: Nvar * 2 the range of each variable [min,max]
% labels: Nvar *1 , cell format, the label of variable
[n,group]=size(alldata); % get the number of variable and group
group_color=jet(group); % random assign fitted color for each group
% plot radial map
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
hold on
text_around(510*cos(theta),510*sin(theta),labels{i},theta); % set label
theta_last=theta;
end
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 xmaxx
maxx=x+dx;
end
if ymaxy
maxy=y+dy;
end
end
axis([minx-50,maxx+50,miny-20,maxy+20]);
% set tick
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
for g=1:group
data=alldata(:,g);
adj_data=zeros(n,1);
point=zeros(n,2);
for i=1:n
theta=2*pi/n*i+pi/2;
if 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)];
end
plot([point(:,1);point(1,1)],[point(:,2);point(1,2)],'k-','linewidth',0.2);
H(g) = fill(point(:,1),point(:,2),group_color(g,:));
alpha(0.5);
end
legend(H,num2str([1:group]'));
end
% write text function
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
end