当前位置:   article > 正文

MATLAB | 情人节画个花瓣venn图?

MATLAB | 情人节画个花瓣venn图?

之前七夕节情人节各种花,相册,爱心啥的都快画够了,今年画个花瓣韦恩图?


花瓣上的数字是仅属于该类的样本数,而中心的数字是属于每一类的样本数


教程部分

0 数据准备

% 给组起名t1 t2 t3...t15
setName = compose('t%d', 1:15);

% 随机生成数据
Data = [rand(200, 15) > .1; rand(200, 15) > .85];

% 设置字体
numFont = {'FontSize', 14, 'FontName', 'Times New Roman'};
labFont = {'FontSize', 16, 'FontName', 'Times New Roman'};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

其中Data数据是个0-1矩阵,矩阵每一行代表一个样本,如果第n列是1代表该样本属于第n类,是0则代表不属于。


1 配色

CList = lines(15);
% C = flowerPlotColor();
% CList = C.CList631;
  • 1
  • 2
  • 3

这里直接取MATLAB自带的lines配色,当然我也准备了很多其他配色,可以复制下来用:

function C=flowerPlotColor
C.CList1877 = [
    0.2431    0.3294    0.5882
    0.5098    0.5647    0.7333
         0    0.5569    0.8078
    0.3490    0.7804    0.9216
    0.0275    0.4431    0.5294
    0.4157    0.6667    0.7176
    0.0392    0.5647    0.5255
    0.3294    0.7490    0.7176
    0.5569    0.1255    0.2627
    0.7373    0.4784    0.5608
    0.8784    0.3765    0.4941
    0.9255    0.6275    0.6980
    0.9961    0.6275    0.5647
    0.9961    0.8118    0.7804
    0.7216    0.7373    0.7569
    0.8824    0.8863    0.8980];
C.CList1514 = [
    0.4078    0.5647    0.8157
    0.9098    0.7843    0.6588
    0.9725    0.8784    0.7216
    0.9725    0.9725    0.9725
    0.2196    0.3137    0.4392
    0.5333    0.6588    0.9725
         0         0         0
    0.5961    0.5961    0.6902
    0.3451    0.4706    0.5647
    0.8471    0.8471    0.9725
    0.7216    0.7216    0.8157
    0.8157    0.6902    0.5647
    0.7216    0.5961    0.4706
    0.2510    0.4078    0.6275
    0.7216    0.7843    0.9725];
C.CList1596 = [
    0.2549    0.5490    0.9412
    0.9882    0.7059    0.2549
    0.8784    0.2510    0.0392
    0.0196    0.3922    0.5725
    0.7490    0.7490    0.7490
    0.1020    0.2314    0.4118
    1.0000    0.8902    0.5098
    0.0706    0.6118    0.8667
    0.7922    0.4196    0.2941
         0    0.3608    0.8588
    0.9529    0.8235    0.5333
    0.3137    0.3882    0.5059
    0.9451    0.7255    0.6588
    0.8784    0.5137    0.0392
    0.4706    0.5765    0.7451];
C.CList1470 = [
    0.1882    0.4078    0.5961
    0.2824    0.5020    0.6902
    0.9098    0.8157    0.6275
    0.7843    0.6902    0.5020
    0.3765    0.5961    0.7843
    0.1255    0.1882    0.2824
         0         0         0
    0.4078    0.3765    0.2824
    0.5961    0.5333    0.3765
    0.5647    0.7529    0.9098
    0.2196    0.3137    0.4706
    0.6902    0.7216    0.7529
    0.9725    0.9725    0.9725
    0.5020    0.5333    0.5961
    0.9725    0.9098    0.7843];
C.CList1827 = [
    1.0000    0.6118         0
    0.9686    0.7412    0.3529
    1.0000    0.8000    0.6000
    1.0000    1.0000    0.2000
    1.0000    1.0000    0.6118
    0.8039    0.3882    0.3882
    0.8000    0.6000    0.8000
    1.0000    0.6196    0.3882
    0.3922    0.4275    0.8000
    0.6118    0.6118    1.0000
    0.2000    0.6000    1.0000
    0.6000    0.8000    1.0000
    1.0000    1.0000    0.8000
    0.6941    0.5843    0.4784
    0.9294    0.5333    0.2980
    0.9608    0.9294         0
    0.8667    1.0000    1.0000];
C.CList1607 = [
    0.8863    0.5686    0.5686
    0.6000    0.8667    0.5725
    0.5765    0.8471    0.7255
    0.5804    0.7686    0.8275
    0.5804    0.6039    0.8078
    0.7020    0.5804    0.8000
    0.8000    0.5882    0.6941
    0.8000    0.6431    0.6000
    0.8745    0.8980    0.5725
    1.0000    0.6471    0.3765
    0.4196    1.0000    0.3882
    0.3961    1.0000    0.8000
    0.3961    0.7686    1.0000
    0.3961    0.4196    1.0000
    0.6784    0.3961    1.0000
    1.0000    0.3961    0.9569
    1.0000    0.3961    0.5176
    1.0000    0.3961    0.3961];
C.CList631 = [
    0.1059    0.6392    0.7765
    0.1725    0.7098    0.7529
    0.1882    0.7373    0.6784
    0.1294    0.6902    0.5294
    0.2000    0.6510    0.3608
    0.3412    0.6392    0.2157
    0.6353    0.7137    0.1529
    0.8353    0.7333    0.1294
    0.9725    0.7137    0.1255
    0.9725    0.5725    0.0902
    0.9412    0.4039    0.0980
    0.8784    0.2039    0.1490
    0.9647    0.2863    0.4431
    0.9882    0.4431    0.6196
    0.9216    0.4510    0.7020
    0.8078    0.4118    0.7451
    0.6353    0.4275    0.7608
    0.4706    0.4510    0.7529
    0.3098    0.4863    0.7294];
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




2 数据处理及图窗生成

% 数据计算
cT = linspace(0, 2*pi, 200);
cX = cos(cT).*8 + 8;
cY = sin(cT).*3;
cXY = [cX;cY];
setNum = size(Data, 2);
rT = 2*pi./setNum;
rM = [cos(rT),-sin(rT); sin(rT),cos(rT)];

uniq = sum(Data.*(sum(Data,2) == 1),1);
core = sum(sum(Data,2) == setNum);

figure('Units', 'normalized', 'Position', [.1,.1,.5,.8]);
ax=gca;
ax.NextPlot = 'add';
ax.DataAspectRatio = [1,1,1];
ax.XColor = 'none';
ax.YColor = 'none';
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

3 绘制花

% 绘制椭圆花瓣
for i = 0:setNum-1
    iXY = rM^i*cXY;
    fill(iXY(1,:), iXY(2,:), CList(i+1,:), 'FaceAlpha', .4, 'EdgeColor', 'none');
end
% 绘制白色边缘线
if 1
for i = 0:setNum-1
    iXY = rM^i*cXY;
    plot(iXY(1,:), iXY(2,:), 'Color', 'w', 'LineWidth', 1.2);
end
end
fill(cos(cT).*2.3, sin(cT).*2.3, [1,1,1], 'EdgeColor', 'none')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13


4 绘制文本

% 绘制文本信息
for i = 1:setNum
    tR = (i-1)*rT/pi*180;
    if tR>=0 && tR<=180
        tR = tR-90;
    else
        tR = tR+90;
    end
    text(cos((i-1)*rT).*13, sin((i-1)*rT).*13, num2str(uniq(i)),...
        'HorizontalAlignment', 'center',...
        'VerticalAlignment', 'middle',...
        'Rotation', tR,...
        numFont{:})
    text(cos((i-1)*rT).*17, sin((i-1)*rT).*17, setName{i},...
        'HorizontalAlignment', 'center',...
        'VerticalAlignment', 'middle',...
        'Rotation', tR,...
        labFont{:})
end
text(0, 0, {'core';num2str(core)},...
    'HorizontalAlignment', 'center',...
    'VerticalAlignment', 'middle',...
    labFont{:})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23


完整代码

% flowerPlotDemo

% 给组起名t1 t2 t3...t15
setName = compose('t%d', 1:15);

% 随机生成数据
Data = [rand(200, 15) > .1; rand(200, 15) > .85];

% 设置字体
numFont = {'FontSize', 14, 'FontName', 'Times New Roman'};
labFont = {'FontSize', 16, 'FontName', 'Times New Roman'};

% 设置配色
CList = lines(15);
% C = flowerPlotColor();
% CList = C.CList631;


% =========================================================
% 绘图部分代码
% ---------------------------------------------------------
% 数据计算
cT = linspace(0, 2*pi, 200);
cX = cos(cT).*8 + 8;
cY = sin(cT).*3;
cXY = [cX;cY];
setNum = size(Data, 2);
rT = 2*pi./setNum;
rM = [cos(rT),-sin(rT); sin(rT),cos(rT)];

uniq = sum(Data.*(sum(Data,2) == 1),1);
core = sum(sum(Data,2) == setNum);

figure('Units', 'normalized', 'Position', [.1,.1,.5,.8]);
ax=gca;
ax.NextPlot = 'add';
ax.DataAspectRatio = [1,1,1];
ax.XColor = 'none';
ax.YColor = 'none';

% 绘制椭圆花瓣
for i = 0:setNum-1
    iXY = rM^i*cXY;
    fill(iXY(1,:), iXY(2,:), CList(i+1,:), 'FaceAlpha', .4, 'EdgeColor', 'none');
end
% 绘制白色边缘线
if 1
for i = 0:setNum-1
    iXY = rM^i*cXY;
    plot(iXY(1,:), iXY(2,:), 'Color', 'w', 'LineWidth', 1.2);
end
end
fill(cos(cT).*2.3, sin(cT).*2.3, [1,1,1], 'EdgeColor', 'none')

% 绘制文本信息
for i = 1:setNum
    tR = (i-1)*rT/pi*180;
    if tR>=0 && tR<=180
        tR = tR-90;
    else
        tR = tR+90;
    end
    text(cos((i-1)*rT).*13, sin((i-1)*rT).*13, num2str(uniq(i)),...
        'HorizontalAlignment', 'center',...
        'VerticalAlignment', 'middle',...
        'Rotation', tR,...
        numFont{:})
    text(cos((i-1)*rT).*17, sin((i-1)*rT).*17, setName{i},...
        'HorizontalAlignment', 'center',...
        'VerticalAlignment', 'middle',...
        'Rotation', tR,...
        labFont{:})
end
text(0, 0, {'core';num2str(core)},...
    'HorizontalAlignment', 'center',...
    'VerticalAlignment', 'middle',...
    labFont{:})
  • 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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/135486
推荐阅读
相关标签
  

闽ICP备14008679号