生活随笔
收集整理的這篇文章主要介紹了
MATLAB绘制雷达图/蜘蛛图
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
雷達圖/蜘蛛圖
雷達圖(Radar Chart) 是以從同一點開始的軸上表示的三個或更多個定量變量的二維圖表的形式顯示多變量數據的圖形方法。軸的相對位置和角度通常是無信息的。 雷達圖也稱為網絡圖,蜘蛛圖,星圖,蜘蛛網圖,不規則多邊形,極坐標圖或Kiviat圖。它相當于平行坐標圖,軸徑向排列。
1 方法一
函數來源為MATLAB | 如何使用MATLAB繪制雷達圖(蜘蛛圖)
1.1 調用函數
名稱說明備注
| ‘Type’ | 用于指定每個軸的標簽 | [‘Line’(默認)/‘Patch’] |
1.2 案例
1.2.1 案例1:填充型
成圖如下所示:
MATLAB實現代碼如下:
clc
close all
clear
%% 導入數據
pathFigure
= '
.\Figures\'
;%% Example
1
X
= randi([2,8],[4,7])+rand([4,7]);figure(1)
RC
= radarChart(X
,'Type
','Patch'
);
RC
.RLim
= [2,10]; % 范圍
RC
.RTick
= [2,8:1:10]; % 刻度線
RC
.PropName
= {'Label1
','Label2
','Label3
','Label4
','Label5
','Label6
','Label7'
};
RC
.ClassName
= {'A','B','C','D'};
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.ClassNumRC
.setPatchN(n
,'FaceColor'
,colorList(n
,:),'EdgeColor'
,colorList(n
,:))
endRC
.setThetaTick('LineWidth'
,2,'Color'
,[.6,.6,.8]); % theta軸顏色設置
RC
.setRTick('LineWidth'
,1.5,'Color'
,[.8,.6,.6]); % R軸顏色設置
RC
.setPropLabel('FontSize'
,15,'FontName
','Times New Roman
','Color'
,[0,0,0]) % 屬性標簽
RC
.setRLabel('FontSize'
,15,'FontName
','Times New Roman
','Color'
,[.8,0,0]) % R刻度標簽
% RC
.setBkg('FaceColor'
,[0.8,0.8,0.8]) % 圓形背景顏色
% RC
.setRLabel('Color
','none'
) % 圓形背景顏色str
= strcat(pathFigure
, "Figure1", '
.tiff'
);
print(gcf
, '
-dtiff'
, '
-r600'
, str
);
1.2.2 案例2:線型
成圖如下所示:
MATLAB實現代碼如下:
clc
close all
clear
%% 導入數據
pathFigure
= '
.\Figures\'
;%% Example
2
X
= randi([2,8],[4,7])+rand([4,7]);figure(2)
RC
=radarChart(X
,'Type
','Line'
);
RC
.PropName
= {'Label1
','Label2
','Label3
','Label4
','Label5
','Label6
','Label7'
};
RC
.ClassName
= {'A','B','C','D'};
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.ClassNumRC
.setPatchN(n
,'Color'
,colorList(n
,:),'MarkerFaceColor'
,colorList(n
,:))
endRC
.setThetaTick('LineWidth'
,2,'Color'
,[.6,.6,.8]); % theta軸顏色設置
RC
.setRTick('LineWidth'
,1.5,'Color'
,[.8,.6,.6]); % R軸顏色設置
RC
.setPropLabel('FontSize'
,15,'FontName
','Times New Roman
','Color'
,[0,0,0]) % 屬性標簽
RC
.setRLabel('FontSize'
,15,'FontName
','Times New Roman
','Color'
,[.8,0,0]) % R刻度標簽
% RC
.setBkg('FaceColor'
,[0.8,0.8,0.8]) % 圓形背景顏色
% RC
.setRLabel('Color
','none'
) % 圓形背景顏色str
= strcat(pathFigure
, "Figure2", '
.tiff'
);
print(gcf
, '
-dtiff'
, '
-r600'
, str
);
2 方法二
函數來源為MATLAB幫助-spider_plot
2.1 調用函數
語法(Syntax):
spider_plot(P
)spider_plot
(P, Name, Value, ...)h
= spider_plot(_
)
輸入變量:
- P:用于繪制蜘蛛圖的數據點。行是數據組,列是數據點。如果沒有指定軸標簽和軸限制,則自動生成。[向量|矩陣]
輸出變量:
名稱-值對參數(Name-Value Pair Arguments):
名稱說明備注
| AxesLabels | 用于指定每個軸的標簽 | [自動生成(默認)/單元格的字符串/ ‘none’] |
| AxesInterval | 用于更改顯示在網頁之間的間隔數 | [3(默認值)/ integer] |
| AxesPrecision | 用于更改軸上顯示的值的精度級別 | [1(默認)/ integer / vector] |
| AxesDisplay | 用于更改顯示軸文本的軸數。'None’或’one’可用于簡化規范化數據的圖形外觀 | [‘none’(默認)/ “沒有”/“一”/“數據”/“data-percent”] |
2.2 案例
2.2.1 案例1:填充型
成圖如下所示:
MATLAB實現代碼如下:
clc
close all
clear
%% 導入數據
pathFigure
= '
.\Figures\'
;
%% Example
1
% Initialize data
points
D1
= [5 3 9 1 2];
D2
= [5 8 7 2 9];
D3
= [8 2 1 4 6];
P
= [D1
; D2
; D3
];% Spider
plot
figure(1)
h
= spider_plot(P
,...'AxesLabels'
, {'S1'
, 'S2'
, 'S3'
, 'S4'
, 'S5'
},...'FillOption'
, {'
on'
, '
on'
, 'off'
},...'FillTransparency'
, [0.2, 0.1, 0.1],...'AxesLimits'
, [1, 2, 1, 1, 1; 10, 8, 9, 5, 10],... % [min
axes limits
; max
axes limits
]'AxesPrecision'
, [0, 1, 1, 1, 1],...'LineStyle'
, {'
--'
, '-', '
--'
},...'LineWidth'
, [1, 2, 3],...'AxesFont'
, 'Times New Roman'
,...'LabelFont'
, 'Times New Roman'
,...'AxesFontSize'
, 12,...'LabelFontSize'
, 12,...'AxesLabelsEdge'
, 'none'
);% Legend
settings
hl
= legend('D1'
, 'D2'
, 'D3'
, 'Location'
, 'northeast'
);
set(hl
,'Box
','off
','FontSize'
,14,'Fontname'
, 'Times New Roman'
);str
= strcat(pathFigure
, "Figure1", '
.tiff'
);
print(gcf
, '
-dtiff'
, '
-r600'
, str
);
2.2.2 案例2:線型
成圖如下所示:
MATLAB實現代碼如下:
clc
close all
clear
%% 導入數據
pathFigure
= '
.\Figures\'
;
%% Example
2
% Initialize data
points
D1
= [5 3 9 1 2 2 9 3 1 9 8 7 2 3 6];
D2
= [5 8 7 2 9 7 6 4 8 9 2 1 8 2 4];
D3
= [8 2 1 4 6 1 8 4 2 3 7 5 6 1 6];
P
= [D1
; D2
; D3
];% Spider
plot
spider_plot(P
,...'AxesLimits'
, [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;...10 10 10 10 10 10 10 10 10 10 10 10 10 10 10],...'AxesInterval'
, 5,...'AxesDisplay'
, 'one'
,...'AxesPrecision'
, 0,...'AxesLabelsRotate'
, '
on'
,...'AxesLabelsOffset'
, 0.1,...'AxesRadial'
, 'off'
,...'AxesFont'
, 'Times New Roman'
,...'LabelFont'
, 'Times New Roman'
,...'AxesFontSize'
, 12,...'LabelFontSize'
, 12,...'AxesLabelsEdge'
, 'none'
);% Legend
settings
hl
= legend('D1'
, 'D2'
, 'D3'
, 'Location'
, 'northeast'
);
set(hl
,'Box
','off
','FontSize'
,14,'Fontname'
, 'Times New Roman'
);str
= strcat(pathFigure
, "Figure2", '
.tiff'
);
print(gcf
, '
-dtiff'
, '
-r600'
, str
);
2.2.3 案例3:繪制各月降水量
成圖如下所示:
MATLAB繪圖代碼如下:
clc
close all
clear
%% 導入數據
pathFigure
= '
.\Figures\'
;%% 開始繪圖
figureUnits
= 'centimeters'
;
figureWidth
= 30;
figureHeight
= 15;figure(1)
set(gcf
, 'Units'
, figureUnits
, 'Position'
, [0 0 figureWidth figureHeight
]);pos1
= [0.05 0.1 0.3 0.8];
subplot('Position'
,pos1
)
hold on;
box on;
spider_plot(PArea
,...'AxesLabels'
, {'Jan
.'
, 'Feb
.'
, 'Mar
.'
, 'Apr
.'
, 'May
','Jun
.'
, 'Jul
.'
, 'Aug
.'
, 'Sep
.'
, 'Oct
.'
, 'Nov
.'
, 'Dec
.'
},...'AxesLimits'
, [ones(1,12)*30 ; ones(1,12)*48 ],...'AxesInterval'
, 5,...'AxesDisplay'
, 'one'
,...'AxesPrecision'
, 0,...'AxesLabelsRotate'
, 'off'
,...'AxesLabelsOffset'
, 0.1,...'AxesRadial'
, '
on'
,...'AxesFont'
, 'Times New Roman'
,...'LabelFont'
, 'Times New Roman'
,...'AxesFontSize'
, 12,...'LabelFontSize'
, 12,...'AxesLabelsEdge'
, 'none'
);
text( '
string'
, "\fontname{Times New Roman}(a)\fontname{宋體}各月降水量\fontname{Times New Roman}/mm", 'Units
','normalized
','position'
,[0.02,1.05], 'FontSize'
,14,'FontWeight
','Bold'
); pos2
= [0.43 0.15 0.56 0.7];
subplot('Position'
,pos2
)
hold on;
box on;
h(1) = plot(PAreaYear
,'
-o
','LineWidth'
,1.5,'color'
,[77,133,189]/255,'MarkerEdgeColor'
,[77,133,189]/255,'MarkerFaceColor'
,[77,133,189]/255,'Markersize'
,5);
h(2) = plot(1:nYear
, PAreaYearfit
,'
--','color'
,[40 120 181]/255,'LineWidth'
,1);
xlabel("\fontname{宋體}\fontsize{15}年份",'FontName
','宋體
','FontSize'
,12); % 后續調整坐標標題
ylabel("\fontname{宋體}\fontsize{15}降水\fontname{Times New Roman}\fontsize{15}/mm",'FontSize'
,12); % 后續調整坐標標題
text( '
string'
, "\fontname{Times New Roman}(b)\fontname{宋體}年降水", 'Units
','normalized
','position'
,[0.02,1.05], 'FontSize'
,14,'FontWeight
','Bold'
);
set(gca
,'xlim'
,[0 nYear
+1],'xtick'
,[1:5:nYear
+1],'xticklabel'
, [yearStart
:5:yearEnd
] ,'FontSize'
,12,'FontName
','Times New Roman
','XMinorTick
','on'
);
text( nYear
/2-3.5,550 ,"y= "+roundn( P(1,1),-4) +"x+"+roundn( P(1,2),-4) , 'color
','k'
, 'FontSize'
,12,'FontName
','Times New Roman'
);
text( nYear
/2-3.5,535 ,"R^2= "+ roundn(R
,-4) , 'color
','k'
, 'FontSize'
,12,'FontName
','Times New Roman'
);
ax
= gca
;
ax
.XAxis
.MinorTickValues
= 1:1:nYear
+1;
set(gca
,'ylim'
,[300 650],'ytick'
,[300:50:620],'yticklabel'
,[300:50:620],'FontSize'
,12,'FontName
','Times New Roman'
);
hl
= legend(h([1 2]), "年降水","線性(年降水)" );
set(hl
,'Box
','off
','location
','NorthEast
','NumColumns'
,2,'FontSize'
,12,'FontName
','宋體'
);
set(gca
,'Layer
','top'
);str
= strcat(pathFigure
, "Figure1", '
.tiff'
);
print(gcf
, '
-dtiff'
, '
-r600'
, str
);
參考
1.MATLAB | 如何使用MATLAB繪制雷達圖(蜘蛛圖)
2.MATLAB幫助-spider_plot
總結
以上是生活随笔為你收集整理的MATLAB绘制雷达图/蜘蛛图的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。