MATLAB绘制雷达图并导出矢量图到Visio编辑(论文用图)

MATLAB绘制雷达图并导出矢量图到Visio编辑(论文用图)


前言: matlab绘制雷达图

  雷达图(Radar Chart)又被叫做蜘蛛网图(Spider Chart),适用于显示三个或更多的维度的变量。雷达图是以在同一点开始的轴上显示的三个或更多个变量的二维图表的形式来显示多元数据的方法,其中轴的相对位置和角度通常是无意义的。

  雷达图的每个变量都有一个从中心向外发射的轴线,所有的轴之间的夹角相等,同时每个轴有相同的刻度,将轴到轴的刻度用网格线链接作为辅助元素,连接每个变量在其各自的轴线的数据点成一条多边形[1]。

在这里插入图片描述

  下面第一部分是MATLAB绘制雷达图,第二部分是MATLAB到处矢量图设置,以便于到Visio编辑

tips: 操作环境: Win10, MATLAB 2020a


一、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 integer
if floor(axes_interval) ~= axes_interval || floor(axes_precision) ~= axes_precision
    error('Error: Please enter in an integer for the axes properties.');
end
% Check if axes properties are positive
if axes_interval < 1 || axes_precision < 1
    error('Error: Please enter value greater than one for the axes properties.');
end
% Check if the labels are the same number as the number of points
if length(P_labels) ~= num_of_points
    error('Error: Please make sure the number of labels is the same as the number of points.');
end
% Pre-allocation
max_values = zeros(1, num_of_points);
min_values = zeros(1, num_of_points);
axis_increment = zeros(1, num_of_points);
% Normalized axis increment
normalized_axis_increment = 1/axes_interval;
% Iterate through number of points
for ii = 1:num_of_points
    % Group of points
    group_points = P(:, ii);
    
    % Max and min value of each group
    max_values(ii) = max(group_points);
    min_values(ii) = min(group_points);
    range = max_values(ii) - min_values(ii);
    
    % Axis increment
    axis_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 increment
    P(:, ii) = P(:, ii) + normalized_axis_increment;
end
%%% Polar Axes %%%
% Polar increments
polar_increments = 2*pi/num_of_points;
% Normalized  max limit of axes
axes_limit = 1;
% Shift axes limit by one axis increment
axes_limit = axes_limit + normalized_axis_increment;
% Polar points
radius = [0; axes_limit];
theta = 0:polar_increments:2*pi;
% Convert polar to cartesian coordinates
[x_axes, y_axes] = pol2cart(theta, radius);
% Plot polar axes
grey = [1, 1, 1] * 0.5;
h = line(x_axes, y_axes,...
    'LineWidth', 1,...
    'Color', grey);
% Iterate through all the line handles
for ii = 1:length(h)
    % Remove polar axes from legend
    h(ii).Annotation.LegendInformation.IconDisplayStyle = 'off';
end
%%% Polar Isocurves %%%
% Shifted axes interval
shifted_axes_interval = axes_interval+1;
% Incremental radius
radius = (0:axes_limit/shifted_axes_interval:axes_limit)';
% Convert polar to cartesian coordinates
[x_isocurves, y_isocurves] = pol2cart(theta, radius);
% Plot polar isocurves
hold on;
h = plot(x_isocurves', y_isocurves',...
    'LineWidth', 1,...
    'Color', grey);
% Iterate through all the plot handles
for ii = 1:length(h)
    % Remove polar isocurves from legend
    h(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 necessary
repeat_colors = fix(row_of_points/size(colors, 1))+1;
colors = repmat(colors, repeat_colors, 1);
%%% Data Points %%%
% Iterate through all the rows
for ii = 1:row_of_points
    % Convert polar to cartesian coordinates
    [x_points, y_points] = pol2cart(theta(1:end-1), P(ii, :));
    
    % Make points circular
    x_circular = [x_points, x_points(1)];
    y_circular = [y_points, y_points(1)];
    
    % Plot data points
    plot(x_circular, y_circular,...
        'Color', colors(ii, :),...
        'MarkerFaceColor', colors(ii, :),...
        varargin{:});
end
%%% Axis Properties %%%
% Figure background
fig = gcf;
fig.Color = 'white';
% Iterate through all the number of points
for hh = 1:num_of_points
    % Shifted min value
    shifted_min_value = min_values(hh)-axis_increment(hh);
    
    % Axis label for each row
    row_axis_labels = (shifted_min_value:axis_increment(hh):max_values(hh))';
    
    % Iterate through all the isocurve radius
    for ii = 2:length(radius)
        % Display axis text for each isocurve
        text(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');
    end
end
% Label points
x_label = x_isocurves(end, :);
y_label = y_isocurves(end, :);
% Shift axis label
shift_pos = 0.07;
% Iterate through each label
for ii = 1:num_of_points
    % Angle of point in radians
    theta_point = theta(ii);
    
    % Find out which quadrant the point is in
    if theta_point == 0
        quadrant = 0;
    elseif theta_point == pi/2
        quadrant = 1.5;
    elseif theta_point == pi
        quadrant = 2.5;
    elseif theta_point == 3*pi/2
        quadrant = 3.5;
    elseif theta_point == 2*pi
        quadrant = 0;
    elseif theta_point > 0 && theta_point < pi/2
        quadrant = 1;
    elseif theta_point > pi/2 && theta_point < pi
        quadrant = 2;
    elseif theta_point > pi && theta_point < 3*pi/2
        quadrant = 3;
    elseif theta_point > 3*pi/2 && theta_point < 2*pi
        quadrant = 4;
    end
    
    % Adjust text alignment information depending on quadrant
    switch quadrant
        case 0
            horz_align = 'left';
            vert_align = 'middle';
            x_pos = shift_pos;
            y_pos = 0;
        case 1
            horz_align = 'left';
            vert_align = 'bottom';
            x_pos = shift_pos;
            y_pos = shift_pos;
        case 1.5
            horz_align = 'center';
            vert_align = 'bottom';
            x_pos = 0;
            y_pos = shift_pos;
        case 2
            horz_align = 'right';
            vert_align = 'bottom';
            x_pos = -shift_pos;
            y_pos = shift_pos;
        case 2.5
            horz_align = 'right';
            vert_align = 'middle';
            x_pos = -shift_pos;
            y_pos = 0;
        case 3
            horz_align = 'right';
            vert_align = 'top';
            x_pos = -shift_pos;
            y_pos = -shift_pos;
        case 3.5
            horz_align = 'center';
            vert_align = 'top';
            x_pos = 0;
            y_pos = -shift_pos;
        case 4
            horz_align = 'left';
            vert_align = 'top';
            x_pos = shift_pos;
            y_pos = -shift_pos;
    end
    
    % Display text label
    text(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 limits
axis square;
axis([-axes_limit, axes_limit, -axes_limit, axes_limit]);
axis off;
end

在这里插入图片描述

注: 上面的源代码也可以在github下载,链接为
NewGuy012/spider_plot [2],源代码里面有很多,单纯为了应用的话上述代码足矣。


Step2 : 在测试文件中输入测试代码测试(后有测试代码说明)

在test.m 中输入如下代码:

%% Example Script %%%
% Clear workspace
close all;
clearvars;
clc;
 
% Point properties
num_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的cell
P_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] https://vis.baidu.com/chartusage/radar/

[2] https://github.com/NewGuy012/spider_plot

[3] https://zhuanlan.zhihu.com/p/65116358

[4] https://ww2.mathworks.cn/matlabcentral/fileexchange/59561-spider_plot/


心有所期,全力以赴,定有所成! 加油吧

Author : 李光辉
date : Mon Jan 25 18:00:23 CST 2021
blog ID: Kevin_8_Lee
blog site : https://blog.csdn.net/Kevin_8_Lee

  • 11
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值