MATLAB绘制雷达图并导出矢量图到Visio编辑(论文用图)
前言: matlab绘制雷达图
雷达图(Radar Chart)又被叫做蜘蛛网图(Spider Chart),适用于显示三个或更多的维度的变量。雷达图是以在同一点开始的轴上显示的三个或更多个变量的二维图表的形式来显示多元数据的方法,其中轴的相对位置和角度通常是无意义的。
雷达图的每个变量都有一个从中心向外发射的轴线,所有的轴之间的夹角相等,同时每个轴有相同的刻度,将轴到轴的刻度用网格线链接作为辅助元素,连接每个变量在其各自的轴线的数据点成一条多边形[1]。
下面第一部分是MATLAB绘制雷达图,第二部分是MATLAB到处矢量图设置,以便于到Visio编辑
tips:操作环境: Win10, MATLAB a
一、MATLAB绘制雷达图
1.1 操作简介
GitHub一位前辈使用matlab封装了绘制雷达图的代码,打开matlab新建一个.m文件存入代码,再新建一个.m文件调用github下载的代码中的函数即可,具体操作如下:
Step1 : 复制下面代码到自己新建的脚本文件中(.m文件)
tips:最好新建一个文件夹,把新建的两个.m文件都放到这一个文件夹中,其中源代码文件命名为 spider_plot.m, 即与函数名一致。
function spider_plot(P, P_labels, axes_interval, axes_precision, varargin)% Create a spider web or radar plot with an axes specified for each column%% spider_plot(P, P_labels, axes_interval, axes_precision) creates a spider% web plot using the points specified in the array P. The column of P% contains the data points and the rows of P contain the multiple sets of% data points. Each point must be accompanied by a label specified in the% cell P_labels. The number of intervals that separate the axes is% specified by axes_interval. The number of decimal precision points is% specified by axes_precision.% % P - [vector | matrix]% P_labels - [cell of string]% axes_interval - [integer]% axes_precision - [integer]%% spider_plot(P, P_labels, axes_interval, axes_precision, line_spec) works% the same as the function above. Additional line properties can be added% in the same format as the default "plot" function in MATLAB.%% line_spec - [character vector]%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Point Properties %%%% Number of points[row_of_points, num_of_points] = size(P);%%% Error Check %%%% Check if axes properties are an integerif floor(axes_interval) ~= axes_interval || floor(axes_precision) ~= axes_precisionerror('Error: Please enter in an integer for the axes properties.');end% Check if axes properties are positiveif axes_interval < 1 || axes_precision < 1error('Error: Please enter value greater than one for the axes properties.');end% Check if the labels are the same number as the number of pointsif length(P_labels) ~= num_of_pointserror('Error: Please make sure the number of labels is the same as the number of points.');end% Pre-allocationmax_values = zeros(1, num_of_points);min_values = zeros(1, num_of_points);axis_increment = zeros(1, num_of_points);% Normalized axis incrementnormalized_axis_increment = 1/axes_interval;% Iterate through number of pointsfor ii = 1:num_of_points% Group of pointsgroup_points = P(:, ii);% Max and min value of each groupmax_values(ii) = max(group_points);min_values(ii) = min(group_points);range = max_values(ii) - min_values(ii);% Axis incrementaxis_increment(ii) = range/axes_interval;% Normalize points to range from [0, 1]P(:, ii) = (P(:, ii)-min(group_points))/range;% Shift points by one axis incrementP(:, ii) = P(:, ii) + normalized_axis_increment;end%%% Polar Axes %%%% Polar incrementspolar_increments = 2*pi/num_of_points;% Normalized max limit of axesaxes_limit = 1;% Shift axes limit by one axis incrementaxes_limit = axes_limit + normalized_axis_increment;% Polar pointsradius = [0; axes_limit];theta = 0:polar_increments:2*pi;% Convert polar to cartesian coordinates[x_axes, y_axes] = pol2cart(theta, radius);% Plot polar axesgrey = [1, 1, 1] * 0.5;h = line(x_axes, y_axes,...'LineWidth', 1,...'Color', grey);% Iterate through all the line handlesfor ii = 1:length(h)% Remove polar axes from legendh(ii).Annotation.LegendInformation.IconDisplayStyle = 'off';end%%% Polar Isocurves %%%% Shifted axes intervalshifted_axes_interval = axes_interval+1;% Incremental radiusradius = (0:axes_limit/shifted_axes_interval:axes_limit)';% Convert polar to cartesian coordinates[x_isocurves, y_isocurves] = pol2cart(theta, radius);% Plot polar isocurveshold on;h = plot(x_isocurves', y_isocurves',...'LineWidth', 1,...'Color', grey);% Iterate through all the plot handlesfor ii = 1:length(h)% Remove polar isocurves from legendh(ii).Annotation.LegendInformation.IconDisplayStyle = 'off';end%%% Figure Properties %%%colors = [0, 0.4470, 0.7410;...0.8500, 0.3250, 0.0980;...0.9290, 0.6940, 0.1250;...0.4940, 0.1840, 0.5560;...0.4660, 0.6740, 0.1880;...0.3010, 0.7450, 0.9330;...0.6350, 0.0780, 0.1840];% Repeat colors is necessaryrepeat_colors = fix(row_of_points/size(colors, 1))+1;colors = repmat(colors, repeat_colors, 1);%%% Data Points %%%% Iterate through all the rowsfor ii = 1:row_of_points% Convert polar to cartesian coordinates[x_points, y_points] = pol2cart(theta(1:end-1), P(ii, :));% Make points circularx_circular = [x_points, x_points(1)];y_circular = [y_points, y_points(1)];% Plot data pointsplot(x_circular, y_circular,...'Color', colors(ii, :),...'MarkerFaceColor', colors(ii, :),...varargin{:});end%%% Axis Properties %%%% Figure backgroundfig = gcf;fig.Color = 'white';% Iterate through all the number of pointsfor hh = 1:num_of_points% Shifted min valueshifted_min_value = min_values(hh)-axis_increment(hh);% Axis label for each rowrow_axis_labels = (shifted_min_value:axis_increment(hh):max_values(hh))';% Iterate through all the isocurve radiusfor ii = 2:length(radius)% Display axis text for each isocurvetext(x_isocurves(ii, hh), y_isocurves(ii, hh), sprintf(sprintf('%%.%if', axes_precision), row_axis_labels(ii)),...'Units', 'Data',...'Color', 'k',...'FontSize', 10,...'HorizontalAlignment', 'center',...'VerticalAlignment', 'middle');endend% Label pointsx_label = x_isocurves(end, :);y_label = y_isocurves(end, :);% Shift axis labelshift_pos = 0.07;% Iterate through each labelfor ii = 1:num_of_points% Angle of point in radianstheta_point = theta(ii);% Find out which quadrant the point is inif theta_point == 0quadrant = 0;elseif theta_point == pi/2quadrant = 1.5;elseif theta_point == piquadrant = 2.5;elseif theta_point == 3*pi/2quadrant = 3.5;elseif theta_point == 2*piquadrant = 0;elseif theta_point > 0 && theta_point < pi/2quadrant = 1;elseif theta_point > pi/2 && theta_point < piquadrant = 2;elseif theta_point > pi && theta_point < 3*pi/2quadrant = 3;elseif theta_point > 3*pi/2 && theta_point < 2*piquadrant = 4;end% Adjust text alignment information depending on quadrantswitch quadrantcase 0horz_align = 'left';vert_align = 'middle';x_pos = shift_pos;y_pos = 0;case 1horz_align = 'left';vert_align = 'bottom';x_pos = shift_pos;y_pos = shift_pos;case 1.5horz_align = 'center';vert_align = 'bottom';x_pos = 0;y_pos = shift_pos;case 2horz_align = 'right';vert_align = 'bottom';x_pos = -shift_pos;y_pos = shift_pos;case 2.5horz_align = 'right';vert_align = 'middle';x_pos = -shift_pos;y_pos = 0;case 3horz_align = 'right';vert_align = 'top';x_pos = -shift_pos;y_pos = -shift_pos;case 3.5horz_align = 'center';vert_align = 'top';x_pos = 0;y_pos = -shift_pos;case 4horz_align = 'left';vert_align = 'top';x_pos = shift_pos;y_pos = -shift_pos;end% Display text labeltext(x_label(ii)+x_pos, y_label(ii)+y_pos, P_labels{ii},...'Units', 'Data',...'HorizontalAlignment', horz_align,...'VerticalAlignment', vert_align,...'EdgeColor', 'k',...'BackgroundColor', 'w');end% Axis limitsaxis square;axis([-axes_limit, axes_limit, -axes_limit, axes_limit]);axis off;end
注:上面的源代码也可以在github下载,链接为
NewGuy012/spider_plot [2],源代码里面有很多,单纯为了应用的话上述代码足矣。
Step2 : 在测试文件中输入测试代码测试(后有测试代码说明)
在test.m 中输入如下代码:
%% Example Script %%%% Clear workspaceclose all;clearvars;clc;% Point propertiesnum_of_points = 6; % 6项指标row_of_points = 4; % 4中方案对比这两个定义即为这个雷达图定义了范围% indicator 这个变量存放6项指标的名字,便于作为label在雷达图上显示indicator = ["成本经济性" "操作简易性" "安全性" "运输便利度" "技术成熟度" "体积比容量" ];% Random data 这里例子是给的随机数据,我自己根据需要给定了数据,其中各项的对比是根据满分1分给各个方案对特定指标打分的%P = rand(row_of_points, num_of_points);% 下面这个P即为自己定义的变量,是一个4*6的矩阵,% 第i(1<= i <=6)列即表示这四种方案对第i个指标的评分高低对应具体数值P = [0.85 0.8 0.3 0.75 0.87 0.45;0.75 0.35 0.45 0.638 0.7 0.9;0.45 0.65 0.96 0.95 0.3 0.85; 0.3 0.6 0.99 0.98 0.83 0.8]; % Create generic labels 这里是创建通用标签,创建一个6*1的cellP_labels = cell(num_of_points, 1); % 6*1 cell% for循环把每一个指标打印到P_label这个变量中,这样P_label就存放了indicator中的6个指标for ii = 1:num_of_points%P_labels{ii} = sprintf('Label %i', ii);P_labels{ii} = sprintf(indicator{ii});end% Figure properties 绘制图窗,设置图窗特性figure('units', 'normalized', 'outerposition', [0 0.05 1 0.95]);% Axes properties 这里设置轴间隔和小数精度axes_interval = 2;axes_precision = 3; % 保留三位小数% Spider plot 调用函数画图,后面的Marker、LineStyle这些是设置绘制的线条的特性spider_plot(P, P_labels, axes_interval, axes_precision,...'Marker', 'o',...'LineStyle', '-',...'LineWidth', 2,...'MarkerSize', 6);% Title properties 这里设置雷达图的标题title('不同方案特点对比',...'Fontweight', 'bold',...'FontSize', 12);% Legend properties legend设定雷达图的图例,定义显示位置在图形外围、下方legend('show',{'方案一','方案二','方案三','方案四'}, 'Location', 'southoutside');
代码说明: 这个测试代码在 NewGuy012 的代码例子基础上改动了一下,具体含义上述代码已有详细注解。
Step3 : 实际效果
二、MATLAB导出可在Visio编辑的矢量图
2.1 操作简介
Step 1: 打开设置
在运行出实际效果图之后不要关闭那个界面,点击 文件->导出设置
Step 2: 大小设置默认即可
tips:之前看了一篇博客是通过计算算出来了长和宽,但是实际操作后发现会有错位,还是自动大小比较好。
Step 3: 渲染设置
导出为向量格式,dpi设置为300即可。
Step 4: 字体和线条设置
如果代码中设置了线条粗细,就不要再动线条的设置了,字体的设置可以根据需要设置,如果没有用到汉字想让别的字符美观,可以自定义字体名称,设置为 Times New Roman
Step 5: 导出为emf格式
只有导出emf格式才可以在Visio中灵活编辑,可以取消组合,单独编辑每一根线条,非常方便。
Step 6: 导入visio并编辑
打开Visio新建一个框图就行,直接把刚才导出的emf文件拖进去
如下动图所示:
Reference:
[1] /chartusage/radar/
[2] /NewGuy012/spider_plot
[3] /p/65116358
[4] /matlabcentral/fileexchange/59561-spider_plot/
心有所期,全力以赴,定有所成! 加油吧
Author : 李光辉
date : Mon Jan 25 18:00:23 CST
blog ID: Kevin_8_Lee
blog site : /Kevin_8_Lee
如果觉得《MATLAB绘制雷达图并导出矢量图到Visio编辑(论文用图)》对你有帮助,请点赞、收藏,并留下你的观点哦!