当前位置:   article > 正文

MATLAB | 如何使用MATLAB绘制雷达图(蜘蛛图)_matlab雷达图

matlab雷达图

绘制效果


教程部分

基础绘制

数据为数值矩阵即可:

X=randi([2,8],[4,7])+rand([4,7]);
RC=radarChart(X);
RC=RC.draw(); 
  • 1
  • 2
  • 3

改变样式

PatchLine两种样式,通过设置Type属性设置样式:

X=randi([2,8],[4,7])+rand([4,7]);
RC=radarChart(X,'Type','Patch');
RC=RC.draw(); 
  • 1
  • 2
  • 3

添加修改标签添加图例

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();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

修改轴属性

使用:

  • 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]);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

修改标签属性

使用:

  • 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])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

修改圆形背景

使用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')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

修饰多边形

两种类型的多边形分别用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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15


工具函数完整代码

classdef radarChart
% @author : slandarer
% gzh  : slandarer随笔
    properties
        ax=[];Parent=[];arginList={'ClassName','PropName','Type','Parent'}
        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=[];
        PatchHdlL;PatchHdlP;PropLabelHdl;BkgHdl
    end

    methods
        function obj=radarChart(varargin)
            if isa(varargin{1},'matlab.graphics.axis.Axes')
                obj.ax=varargin{1};varargin(1)=[];
            else  
            end
            obj.XData=varargin{1};varargin(1)=[];
            obj.PropNum=size(obj.XData,2);
            obj.ClassNum=size(obj.XData,1);
            obj.RLim=[0,max(max(obj.XData))];

            % 获取其他信息
            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.ax)&&(~isempty(obj.Parent))
                obj.ax=obj.Parent;
            end
            if isempty(obj.ax)
                obj.ax=gca;
            end
            obj.ax.NextPlot='add';
            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(obj.ax,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(obj.ax,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(obj.ax,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);
                obj.PatchHdlP(i)=patch(obj.ax,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);
                obj.PatchHdlL(i)=plot(obj.ax,[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,:));
                switch obj.Type
                    case 'Line',set(obj.PatchHdlP(i),'Visible','off')
                    case 'Patch',set(obj.PatchHdlL(i),'Visible','off')
                    case 'Both'
                end
            end

            % 绘制R标签文本
            tnr=(tn(1)+tn(2))/2;
            for i=1:length(obj.RTick)
                obj.RLabelHdl(i)=text(obj.ax,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(obj.ax,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=setType(obj,Type)
            obj.Type=Type;
            for i=1:size(obj.XData,1)
                set(obj.PatchHdlP(i),'Visible','on')
                set(obj.PatchHdlL(i),'Visible','on')
                switch obj.Type
                    case 'Line',set(obj.PatchHdlP(i),'Visible','off')
                    case 'Patch',set(obj.PatchHdlL(i),'Visible','off')
                end
            end
            delete(findobj('Tag','SRClagend'))
            if isequal(obj.Type,'Line')
                obj.LgdHdl=legend(obj.ax,[obj.PatchHdlL],obj.ClassName,'FontSize',12,'Location','best','Tag','SRClagend');
            else
                obj.LgdHdl=legend(obj.ax,[obj.PatchHdlP],obj.ClassName,'FontSize',12,'Location','best','Tag','SRClagend');
            end
        end

        % 绘制图例
        function obj=legend(obj)
            if isequal(obj.Type,'Line')
                obj.LgdHdl=legend(obj.ax,[obj.PatchHdlL],obj.ClassName,'FontSize',12,'Location','best','Tag','SRClagend');
            else
                obj.LgdHdl=legend(obj.ax,[obj.PatchHdlP],obj.ClassName,'FontSize',12,'Location','best','Tag','SRClagend');
            end
        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)
            switch obj.Type
                case 'Line',set(obj.PatchHdlL(N),varargin{:})
                case 'Patch',set(obj.PatchHdlP(N),varargin{:})
            end
        end

        function setData(obj,Data)
            tn=linspace(0,2*pi,obj.PropNum+1);tn=tn(1:end-1);
            obj.XData=Data;
            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);
                set(obj.PatchHdlL(i),'XData',[XP,XP(1)],'YData',[YP,YP(1)]);
                set(obj.PatchHdlP(i),'XData',[XP,XP(1)],'YData',[YP,YP(1)]);
            end
        end
    end
% @author : slandarer
% gzh : slandarer随笔
% -------------------------------------------------------------------------
end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206

2023-3-19日更新

已支持app designer使用,提供一个示例:

在这里插入图片描述

classdef radarChartApp < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure     matlab.ui.Figure
        ButtonGroup  matlab.ui.container.ButtonGroup
        Button_3     matlab.ui.control.RadioButton
        Button_2     matlab.ui.control.RadioButton
        Button       matlab.ui.control.Button
        UIAxes       matlab.ui.control.UIAxes
    end

    
    properties (Access = private)
        Data=randi([-3,8],[4,7])+rand([4,7]); % Description
        RC
    end

    % Callbacks that handle component events
    methods (Access = private)

        % Code that executes after component creation
        function startupFcn(app)
            app.RC=radarChart(app.UIAxes,app.Data);
            app.RC.RLim=[-5,10];
            app.RC.RTick=[-5,2,8:1:10];
            app.RC=app.RC.draw();
            app.RC=app.RC.legend();
        end

        % Button pushed function: Button
        function ButtonPushed(app, event)
            app.Data=randi([-3,8],[4,7])+rand([4,7]);
            app.RC.setData(app.Data);
        end

        % Selection changed function: ButtonGroup
        function ButtonGroupSelectionChanged(app, event)
            selectedButton = app.ButtonGroup.SelectedObject;
            switch selectedButton.Text
                case '填充',app.RC.setType('Patch')
                case '线条',app.RC.setType('Line')
            end
            
        end
    end

    % Component initialization
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create UIFigure and hide until all components are created
            app.UIFigure = uifigure('Visible', 'off');
            app.UIFigure.Position = [100 100 640 480];
            app.UIFigure.Name = 'MATLAB App';

            % Create UIAxes
            app.UIAxes = uiaxes(app.UIFigure);
            title(app.UIAxes, 'Title')
            xlabel(app.UIAxes, 'X')
            ylabel(app.UIAxes, 'Y')
            zlabel(app.UIAxes, 'Z')
            app.UIAxes.Position = [1 21 446 441];

            % Create Button
            app.Button = uibutton(app.UIFigure, 'push');
            app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true);
            app.Button.BackgroundColor = [0 0.4471 0.7412];
            app.Button.FontName = '宋体';
            app.Button.FontSize = 24;
            app.Button.FontWeight = 'bold';
            app.Button.FontColor = [1 1 1];
            app.Button.Position = [488 380 113 71];
            app.Button.Text = {'生成随机'; '数并绘图'};

            % Create ButtonGroup
            app.ButtonGroup = uibuttongroup(app.UIFigure);
            app.ButtonGroup.SelectionChangedFcn = createCallbackFcn(app, @ButtonGroupSelectionChanged, true);
            app.ButtonGroup.Title = '雷达图类型';
            app.ButtonGroup.FontName = '宋体';
            app.ButtonGroup.FontSize = 18;
            app.ButtonGroup.Position = [488 235 123 120];

            % Create Button_2
            app.Button_2 = uiradiobutton(app.ButtonGroup);
            app.Button_2.Text = '线条';
            app.Button_2.FontName = '宋体';
            app.Button_2.FontSize = 14;
            app.Button_2.Position = [11 49 58 22];
            app.Button_2.Value = true;

            % Create Button_3
            app.Button_3 = uiradiobutton(app.ButtonGroup);
            app.Button_3.Text = '填充';
            app.Button_3.FontName = '宋体';
            app.Button_3.FontSize = 14;
            app.Button_3.Position = [11 21 65 22];

            % Show the figure after all components are created
            app.UIFigure.Visible = 'on';
        end
    end

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = radarChartApp

            % Create UIFigure and components
            createComponents(app)

            % Register the app with App Designer
            registerApp(app, app.UIFigure)

            % Execute the startup function
            runStartupFcn(app, @startupFcn)

            if nargout == 0
                clear app
            end
        end

        % Code that executes before app deletion
        function delete(app)

            % Delete UIFigure when app is deleted
            delete(app.UIFigure)
        end
    end
end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/728663
推荐阅读
相关标签
  

闽ICP备14008679号