如何在 MATLAB 上绘制维恩图?

问题描述

我必须绘制一些维恩图来显示字符串数组之间的交集。让我解释一下:我有一个名称类别,其中包括其他三个互不相交的名称。我想了解这三个中哪个占据了上面提到的宏观类别的最大百分比。我想用 MATLAB 来做,但我发现这个开发环境有点缺乏这样的功能。如果您有任何想法,我将不胜感激。 提前致谢!

解决方法

绘制圆的维恩图

不是最优雅的方式,而是绘制维恩图的一种方法是绘制圆圈并使用 text() 注释/标签填充各个圆圈。不幸的是,在这里我手动放置了标签居中的位置。自动化标签定位可能需要几个额外的步骤,为了简单起见,我将省略这些步骤。为了找到各个数据集的交集,我只是使用了 intersect() 函数。

Venn Diagram Plot

A = {'A','B','C','D','E','F','G','H','I','L'};
B = {'A','D'};
C = {'E','L'};

%Finding the intersections of the arrays%
Intersection_AB = intersect(A,B);
fprintf("Intersection AB: ")
disp(Intersection_AB);
fprintf("\n");

Intersection_BC = intersect(B,C);
fprintf("Intersection BC: ")
disp(Intersection_BC);
fprintf("\n");

Intersection_AC = intersect(A,C);
fprintf("Intersection AC: ")
disp(Intersection_AC);
fprintf("\n");

Intersection_ABC = intersect(Intersection_AB,C);
fprintf("Intersection ABC: ")
disp(Intersection_ABC);
fprintf("\n");
clc;

clf;
Plotting_Interval = 0.01;
Angles_In_Radians = (0: Plotting_Interval: 2*pi);
Circle_Plot = @(X_Offset,Y_Offset,Radius) plot(X_Offset + Radius*cos(Angles_In_Radians),Y_Offset + Radius*sin(Angles_In_Radians));

hold on
%Plotting the 3 circles%
X_Offset_A = 0; Y_Offset_A = 2; Radius_A = 3;
Circle_A = Circle_Plot(X_Offset_A,Y_Offset_A,Radius_A);
fill(Circle_A.XData,Circle_A.YData,'r','FaceAlpha',0.2,'LineWidth',1);

X_Offset_B = -2; Y_Offset_B = -2; Radius_B = 3;
Circle_B = Circle_Plot(X_Offset_B,Y_Offset_B,Radius_B);
fill(Circle_B.XData,Circle_B.YData,'g',1);

X_Offset_C = 2; Y_Offset_C = -2; Radius_C = 3;
Circle_Plot(X_Offset_C,Y_Offset_C,Radius_C);
Circle_C = Circle_Plot(X_Offset_C,Radius_C);
fill(Circle_C.XData,Circle_C.YData,'b',1);
title("Venn Diagram");

%Writing all the labels%
A_Label = strjoin(string(A));
text(X_Offset_A,A_Label,'color','r');

B_Label = strjoin(string(B));
text(X_Offset_B,B_Label,'g');

C_Label = strjoin(string(C));
text(X_Offset_C,C_Label,'b');

AB_Label = strjoin(string(Intersection_AB));
text(-1.2,AB_Label);

BC_Label = strjoin(string(Intersection_BC));
text(0,-2,BC_Label);

AC_Label = strjoin(string(Intersection_AC));
text(1.2,AC_Label);

ABC_Label = strjoin(string(Intersection_ABC));
text(0,ABC_Label);

%Setting the labels to be relative to the centres%
set(findall(gcf,'type','text'),'HorizontalAlignment','center');
axis equal
axis off

使用 MATLAB R2019b 运行

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...