绘制效果
教程部分
基础绘制
改变样式
添加修改标签添加图例
R轴位置及其范围
修改轴属性
修改标签属性
修改圆形背景
修饰多边形
工具函数完整代码
绘制效果 教程部分 基础绘制数据为数值矩阵即可:
X=randi([2,8],[4,7])+rand([4,7]);
RC=radarChart(X);
RC=RC.draw();
改变样式
有Patch
和Line
两种样式,通过设置Type
属性设置样式:
X=randi([2,8],[4,7])+rand([4,7]);
RC=radarChart(X,'Type','Patch');
RC=RC.draw();
添加修改标签添加图例
X=randi([2,8],[4,7])+rand([4,7]);
RC=radarChart(X);
RC.PropName={'建模','实验','编程','总结','撰写','创新','摸鱼'};
RC.ClassName={'同门A','同门B','同门C','同门D'};
RC=RC.draw();
RC.legend();
R轴位置及其范围
范围改为[2,10],并绘制2,8,9,10四条刻度线:
X=randi([2,8],[4,7])+rand([4,7]);
RC=radarChart(X);
RC.RLim=[2,10];
RC.RTick=[2,8:1:10];
RC=RC.draw();
RC.legend();
修改轴属性
使用:
setThetaTick
setRTick
设置轴属性,举个例子,theta轴变为淡红色,R轴变为淡蓝色:
X=randi([2,8],[4,7])+rand([4,7]);
RC=radarChart(X);
RC=RC.draw();
RC.legend();
RC.setThetaTick('LineWidth',2,'Color',[.6,.6,.8]);
RC.setRTick('LineWidth',1.5,'Color',[.8,.6,.6]);
修改标签属性
使用:
setPropLabel
setRLabel
修改属性标签和R刻度标签:
X=randi([2,8],[4,7])+rand([4,7]);
RC=radarChart(X);
RC=RC.draw();
RC.legend();
RC.setPropLabel('FontSize',20,'FontName','Cambria','Color',[0,0,.8])
RC.setRLabel('FontSize',15,'FontName','Cambria','Color',[.8,0,0])
修改圆形背景
使用setBkg
函数修饰背景:
X=randi([2,8],[4,7])+rand([4,7]);
RC=radarChart(X);
RC=RC.draw();
RC.legend();
RC.setBkg('FaceColor',[0,0,.1])
RC.setRLabel('Color','none')
修饰多边形
两种类型的多边形分别用Patch
对象和Line
对象绘制,修饰方式略有区别,不过都是使用setPatchN
函数进行修饰,以下以修改颜色举例:
X=randi([2,8],[4,7])+rand([4,7]);
RC=radarChart(X);
RC=RC.draw();
RC.legend();
colorList=[78 101 155;
138 140 191;
184 168 207;
231 188 198;
253 207 158;
239 164 132;
182 118 108]./255;
for n=1:RC.ClassNum
RC.setPatchN(n,'Color',colorList(n,:),'MarkerFaceColor',colorList(n,:))
end
X=randi([2,8],[4,7])+rand([4,7]);
RC=radarChart(X,'Type','Patch');
RC=RC.draw();
RC.legend();
colorList=[78 101 155;
138 140 191;
184 168 207;
231 188 198;
253 207 158;
239 164 132;
182 118 108]./255;
for n=1:RC.ClassNum
RC.setPatchN(n,'FaceColor',colorList(n,:),'EdgeColor',colorList(n,:))
end
工具函数完整代码
classdef radarChart
% @author : slandarer
% gzh : slandarer随笔
properties
ax;arginList={'ClassName','PropName','Type'}
XData;RTick=[];RLim=[];SepList=[1,1.2,1.5,2,2.5,3,4,5,6,8]
Type='Line';
PropNum;ClassNum
ClassName={};
PropName={};
BC=[198,199,201; 38, 74, 96; 209, 80, 51; 241,174, 44; 12,13,15;
102,194,165; 252,140, 98; 142,160,204; 231,138,195;
166,217, 83; 255,217, 48; 229,196,148; 179,179,179]./255;
% 句柄
ThetaTickHdl;RTickHdl;RLabelHdl;LgdHdl;PatchHdl;PropLabelHdl;BkgHdl
end
methods
function obj=radarChart(varargin)
if isa(varargin{1},'matlab.graphics.axis.Axes')
obj.ax=varargin{1};varargin(1)=[];
else
obj.ax=gca;
end
% 获取版本信息
tver=version('-release');
verMatlab=str2double(tver(1:4))+(abs(tver(5))-abs('a'))/2;
if verMatlab<2017
hold on
else
hold(obj.ax,'on')
end
obj.XData=varargin{1};varargin(1)=[];
obj.PropNum=size(obj.XData,2);
obj.ClassNum=size(obj.XData,1);
obj.RLim=[0,max(obj.XData,[],[1,2])];
% 获取其他信息
for i=1:2:(length(varargin)-1)
tid=ismember(obj.arginList,varargin{i});
if any(tid)
obj.(obj.arginList{tid})=varargin{i+1};
end
end
if isempty(obj.ClassName)
for i=1:obj.ClassNum
obj.ClassName{i}=['class ',num2str(i)];
end
end
if isempty(obj.PropName)
for i=1:obj.PropNum
obj.PropName{i}=['prop ',num2str(i)];
end
end
help radarChart
end
function obj=draw(obj)
obj.ax.XLim=[-1,1];
obj.ax.YLim=[-1,1];
obj.ax.XTick=[];
obj.ax.YTick=[];
obj.ax.XColor='none';
obj.ax.YColor='none';
obj.ax.PlotBoxAspectRatio=[1,1,1];
% 绘制背景圆形
tt=linspace(0,2*pi,200);
obj.BkgHdl=fill(cos(tt),sin(tt),[252,252,252]./255,'EdgeColor',[200,200,200]./255,'LineWidth',1);
% 绘制Theta刻度线
tn=linspace(0,2*pi,obj.PropNum+1);tn=tn(1:end-1);
XTheta=[cos(tn);zeros([1,obj.PropNum]);nan([1,obj.PropNum])];
YTheta=[sin(tn);zeros([1,obj.PropNum]);nan([1,obj.PropNum])];
obj.ThetaTickHdl=plot(XTheta(:),YTheta(:),'Color',[200,200,200]./255,'LineWidth',1);
% 绘制R刻度线
if isempty(obj.RTick)
dr=diff(obj.RLim);
sepR=dr./3;
multiE=ceil(log(sepR)/log(10));
sepR=sepR.*10^(1-multiE);
sepR=obj.SepList(find(sepR<obj.SepList,1)-1)./10^(1-multiE);
sepNum=floor(dr./sepR);
obj.RTick=obj.RLim(1)+(0:sepNum).*sepR;
if obj.RTick(end)~=obj.RLim(2)
obj.RTick=[obj.RTick,obj.RLim];
end
end
obj.RLim(obj.RLim<obj.RLim(1))=[];
obj.RLim(obj.RLim>obj.RLim(2))=[];
XR=cos(tt').*(obj.RTick-obj.RLim(1))./diff(obj.RLim);XR=[XR;nan([1,length(obj.RTick)])];
YR=sin(tt').*(obj.RTick-obj.RLim(1))./diff(obj.RLim);YR=[YR;nan([1,length(obj.RTick)])];
obj.RTickHdl=plot(XR(:),YR(:),'Color',[200,200,200]./255,'LineWidth',1.1,'LineStyle','--');
% 绘制雷达图
for i=1:size(obj.XData,1)
XP=cos(tn).*(obj.XData(i,:)-obj.RLim(1))./diff(obj.RLim);
YP=sin(tn).*(obj.XData(i,:)-obj.RLim(1))./diff(obj.RLim);
switch obj.Type
case 'Line'
obj.PatchHdl(i)=plot([XP,XP(1)],[YP,YP(1)],...
'Color',obj.BC(mod(i-1,size(obj.BC,1))+1,:),'Marker','o',...
'LineWidth',1.8,'MarkerFaceColor',obj.BC(mod(i-1,size(obj.BC,1))+1,:));
case 'Patch'
obj.PatchHdl(i)=patch(XP,YP,obj.BC(mod(i-1,size(obj.BC,1))+1,:),...
'EdgeColor',obj.BC(mod(i-1,size(obj.BC,1))+1,:),'FaceAlpha',.2,...
'LineWidth',1.8);
end
end
% 绘制R标签文本
tnr=(tn(1)+tn(2))/2;
for i=1:length(obj.RTick)
obj.RLabelHdl(i)=text(cos(tnr).*(obj.RTick(i)-obj.RLim(1))./diff(obj.RLim),...
sin(tnr).*(obj.RTick(i)-obj.RLim(1))./diff(obj.RLim),...
sprintf('%.2f',obj.RTick(i)),'FontName','Arial','FontSize',11);
end
% 绘制属性标签
for i=1:obj.PropNum
obj.PropLabelHdl(i)=text(cos(tn(i)).*1.1,sin(tn(i)).*1.1,obj.PropName{i},...
'FontSize',12,'HorizontalAlignment','center');
end
end
% =========================================================================
function obj=setBkg(obj,varargin)
set(obj.BkgHdl,varargin{:})
end
% 绘制图例
function obj=legend(obj)
obj.LgdHdl=legend([obj.PatchHdl],obj.ClassName,'FontSize',12,'Location','best');
end
% 设置图例属性
function obj=setLegend(obj,varargin)
set(obj.LgdHdl,varargin{:})
end
% 设置标签
function obj=setPropLabel(obj,varargin)
for i=1:obj.PropNum
set(obj.PropLabelHdl(i),varargin{:})
end
end
function obj=setRLabel(obj,varargin)
for i=1:length(obj.RLabelHdl)
set(obj.RLabelHdl(i),varargin{:})
end
end
% 设置轴
function obj=setRTick(obj,varargin)
set(obj.RTickHdl,varargin{:})
end
function obj=setThetaTick(obj,varargin)
set(obj.ThetaTickHdl,varargin{:})
end
% 设置patch属性
function obj=setPatchN(obj,N,varargin)
set(obj.PatchHdl(N),varargin{:})
end
end
% @author : slandarer
% gzh : slandarer随笔
end
以上就是Matlab实现绘制雷达图(蜘蛛图)的详细内容,更多关于Matlab雷达图的资料请关注软件开发网其它相关文章!