1. 绘制雷达图
本文根据:MATLAB 雷达图画图函数 修改,对于原文作者表示感谢。
function Draw_radar(data,lim,labels)
%data是要画图的数据(根据数据的个数来确定雷达图的轴数)
%lim是各指标画图上下限范围
%labels是坐标轴名称
n=length(data);%维度
adj_data=zeros(n,1);
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)<lim(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)];
text_around(510*cos(theta),510*sin(theta),labels{i},theta);
end
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.8);
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
minx=x;
elseif x+dx>maxx
maxx=x+dx;
end
if y<miny
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
end
1.1 单数据雷达图
data = [5 8 1 4];
lim = [0,10;0,10;0,10;0,10];
labels = {'1','2','3','4'};
Draw_radar(data,lim,labels);
1.2 多数据雷达图
当数据有多组时,例如:data = [5,8,1,4;7,5,4,8]。
可以绘制[5 8 1 4]之后,再hold on,最后绘制[7,5,4,8]即可。
data = [5 8 1 4];
lim = [0,10;0,10;0,10;0,10];
labels = {'1','2','3','4'};
Draw_radar(data,lim,labels);
hold on;
data = [7,5,4,8];
lim = [0,10;0,10;0,10;0,10];
labels = {'1','2','3','4'};
Draw_radar(data,lim,labels);
当然,最好把上面的操作封装一下,使用起来更方便,封装函数Draw_radar2代码如下:
function Draw_radar2(data,lim,labels)
hold on;
for i=1:size(data,1)
Draw_radar(data(i,:),lim,labels);
end
end
2. legend自由控制
当你想显示legend时,上述的方法可能就会出现如下的效果。
解决方案详情可参考:http://blog.sciencenet.cn/blog-662112-764543.html,笔者从中受到启示,得出下面的解决方案:
通过设置IconDisplayStyle属性为false,通知Matlab不显示该plot语句所绘制图形的legend。
set(get(get(plot句柄,'Annotation'),'LegendInformation'),'IconDisplayStyle','off');
例如:如果只想显示第1,6,11,16,21条线的legend,则可以【伪代码】:
for i = [2:5 7:10 12:15 17:20 22:25]
set(get(get(H(i),'Annotation'),'LegendInformation'),'IconDisplayStyle','off');
end
legend('1','6','11','16','21');
其中,H为所有plot函数的句柄。
另外,调试发现,Draw_radar代码中的第39行,即最后一个plot绘图指令,就是我们要显示legend的线。对于上面的四个维度的数据,虚线一共4*5=20条,坐标轴实线共有4条,那么,第25条曲线就是我们需要显示legend的线。
那么,我们就可以做点什么了。我们可以只设置最后一个plot绘图对象的IconDisplayStyle属性为on,其余均设置为false的方式,实现只显示第最后一条直线的legend的目的。
我们这里对最开始的Draw_radar函数做以下的修改:
- 首先,在Draw_radar函数中保存所有plot函数的句柄到数组H中;
- 接着,为了更能区分不同的曲线,笔者,将线的颜色和类型linestyle作为参数传入到Draw_radar函数中,当linestyle为'r-'时,对应的数据会显示为红色线型;
- 最后,为了更加清晰,将原有的填充中间区域注释掉了。
下面是修改后的Draw_radar和Draw_radar2的完整代码。
function [H] = Draw_radar(data,lim,labels,linestyle)
%data是要画图的数据(根据数据的个数来确定雷达图的轴数)
%lim是各指标画图上下限范围
%labels是坐标轴名称
H={};
n=length(data);%维度
adj_data=zeros(n,1);
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;
H{length(H) + 1} = plot([0,500*cos(theta)],[0,500*sin(theta)],'k-','linewidth',2);
for j=1:5
H{length(H) + 1} = 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)<lim(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)];
text_around(510*cos(theta),510*sin(theta),labels{i},theta);
end
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
H{length(H) + 1} = plot([point(:,1);point(1,1)],[point(:,2);point(1,2)],linestyle,'linewidth',1.5);%绘制
%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
minx=x;
elseif x+dx>maxx
maxx=x+dx;
end
if y<miny
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,'Interpreter','latex','VerticalAlignment','cap','HorizontalAlignment','left','Fontsize',fontsize);
elseif section>1.5*pi
%中对齐
text(x,y,txt,'Interpreter','latex','VerticalAlignment','cap','HorizontalAlignment','center','Fontsize',fontsize);
else
%右对齐
text(x,y,txt,'Interpreter','latex','VerticalAlignment','cap','HorizontalAlignment','right','Fontsize',fontsize);
end
elseif section>pi
%中、右对齐
text(x,y,txt,'Interpreter','latex','VerticalAlignment','middle','HorizontalAlignment','right','Fontsize',fontsize);
elseif section>pi/6
%下对齐
if section>0.5*pi+pi/6
%右对齐
text(x,y,txt,'Interpreter','latex','VerticalAlignment','bottom','HorizontalAlignment','right','Fontsize',fontsize);
elseif section>0.5*pi
%中对齐
text(x,y,txt,'Interpreter','latex','VerticalAlignment','bottom','HorizontalAlignment','center','Fontsize',fontsize);
else
%左对齐
text(x,y,txt,'Interpreter','latex','VerticalAlignment','bottom','HorizontalAlignment','left','Fontsize',fontsize);
end
else
%中、左对齐
text(x,y,txt,'Interpreter','latex','VerticalAlignment','middle','HorizontalAlignment','left','Fontsize',fontsize);
end
end
function Draw_radar2(data,lim,labels,legendlabels)
hold on;
linestyles = {'k-','r-','b-','g-','c-','m-','y-'};
for i=1:size(data,1)
H = Draw_radar(data(i,:),lim,labels,linestyles{i});
for j=1:length(H)-1
set(get(get(H{j},'Annotation'),'LegendInformation'),'IconDisplayStyle','off');
end
end
legend(legendlabels,'Interpreter','latex');
end
注意,这里的linestyle共有7种线型,如果要显示多于7组数据,加入新的线性即可。 调用方式如下:
lim = [0,40;0,50;0,20;0,10;0,200;];
labels = {'$$J_\alpha$$','$$J_\beta$$','$$J_\gamma$$','$$J_\delta$$','$$J_\epsilon$$'};
legendlabels = {'Plan 1','Plan 2','Plan 3'};
data = [25 32 11 7 151;
33 45 11 6 151;
28 12 11 9 162];
Draw_radar2(data,lim,labels,legendlabels);