📜  如何在Python创建和自定义维恩图?

📅  最后修改于: 2022-05-13 01:55:25.063000             🧑  作者: Mango

如何在Python创建和自定义维恩图?

维恩图可用于说明两个或多个组之间的关系。我们可以很容易地看到不同群体之间的共性和差异。在本文中,我们将讨论如何在Python创建和自定义维恩图:

简单的维恩图:

安装:

在您的计算机中安装matplotlib-venn库(这里我们使用了Pycharm工具)转到终端并使用以下命令。



pip install matplotlib-venn

安装库后,创建一个新的Python文件并按照以下程序中的说明导入库:

Python3
# import modules
from matplotlib_venn import venn2 
from matplotlib import pyplot as plt
  
# depict venn diagram
venn2(subsets = (50, 10, 7), set_labels = ('Group A', 'Group B'))
plt.show()


Python3
# import modules
from matplotlib_venn import venn2_unweighted 
from matplotlib import pyplot as plt
  
# depict venn diagram
venn2_unweighted(subsets = (50, 10, 7),
                 set_labels = ('Group A', 
                               'Group B'),
                 set_colors=("orange",
                             "blue"),alpha=0.7)
plt.show()


Python3
# import modules
from matplotlib_venn import venn2,venn2_circles
from matplotlib import pyplot as plt
  
# depict venn diagram
venn2(subsets = (50, 10, 7),
      set_labels = ('Group A', 
                    'Group B'),
      set_colors=("orange",
                  "blue"),alpha=0.7)
  
# add outline
venn2_circles(subsets=(50,10,7)) 
plt.show()


Python3
# import modules
from matplotlib_venn import venn2, venn2_circles
from matplotlib import pyplot as plt
  
# depict venn diagram
venn2(subsets=(50, 10, 7), 
      set_labels=('Group A', 'Group B'),
      set_colors=("orange", "blue"), alpha=0.7)
  
# outline of the circle with defined 
# line style and line width
venn2_circles(subsets=(50, 10, 7), 
              linestyle="dashed", linewidth=2)
plt.show()


Python3
# import modules
from matplotlib_venn import venn2, venn2_circles
from matplotlib import pyplot as plt
  
# depict venn diagram
venn2(subsets=(50, 10, 7), 
      set_labels=('Group A', 'Group B'),
      set_colors=("orange", "blue"), alpha=0.7)
  
# add outline
venn2_circles(subsets=(50, 10, 7), 
              linestyle="dashed", 
              linewidth=2)
  
# assign title of the venn diagram
plt.title("Venn Diagram in geeks for geeks")  
plt.show()


Python3
# import module
from matplotlib_venn import venn3, venn3_circles
from matplotlib import pyplot as plt
  
# depict venn diagram
venn3(subsets=(20, 10, 12, 10, 9, 4, 3), 
      set_labels=('Group A', 'Group B', 'Group C'), 
      set_colors=("orange", "blue", "red"), alpha=0.7)
  
# outline of circle line style and width
venn3_circles(subsets=(20, 10, 12, 10, 9, 4, 3),
              linestyle="dashed", linewidth=2)
  
# title of the venn diagram
plt.title("Venn Diagram in geeks for geeks")
plt.show()


Python3
#import module
from matplotlib_venn import venn3, venn3_circles
from matplotlib import pyplot as plt
  
# depict venn diagram
v = venn3(subsets=(1, 1, 1, 1, 1, 1, 1), 
          set_labels=('A', 'B', 'C'))
  
# set color to defined path id
v.get_patch_by_id("100").set_color("white")
# set text to defined label id
v.get_label_by_id("100").set_text("unknown")
# set text to defined label id "A"
v.get_label_by_id('A').set_text('A new')
  
# add outline
venn3_circles(subsets=(1, 1, 1, 1, 1, 1, 1), 
              linestyle="dashed", linewidth=2)
  
# assign title
plt.title("Venn Diagram in geeks for geeks")
plt.show()


输出:

语句venn2(subsets = (30, 10, 5), set_labels = ('Group A', 'Group B')) 指子集的参数是一个 3 元素列表,其中数字 50、10、7 对应于 Ab、aB、AB。

设置标签 参数允许您在维恩图中标记您的两个组。 matplotlib 库的 pyplot 模块中的show()函数用于显示所有图形。

以下是描述如何创建和自定义维恩图的各种示例:

示例 1:

维恩图根据分配的项目的大小自动调整圆圈的大小。但是,我们可以使用未加权的维恩图禁用此功能,因此无论分配的项目如何,圆圈都以相同的大小显示。

维恩图的默认颜色是红色和绿色,现在我们将使用set_colors参数自定义橙色和蓝色。 alpha参数用于控制透明度。

蟒蛇3

# import modules
from matplotlib_venn import venn2_unweighted 
from matplotlib import pyplot as plt
  
# depict venn diagram
venn2_unweighted(subsets = (50, 10, 7),
                 set_labels = ('Group A', 
                               'Group B'),
                 set_colors=("orange",
                             "blue"),alpha=0.7)
plt.show()

输出:

示例 2:

我们可以自定义它在以下程序中显示的加权维恩图上工作的圆圈笔记的轮廓。



蟒蛇3

# import modules
from matplotlib_venn import venn2,venn2_circles
from matplotlib import pyplot as plt
  
# depict venn diagram
venn2(subsets = (50, 10, 7),
      set_labels = ('Group A', 
                    'Group B'),
      set_colors=("orange",
                  "blue"),alpha=0.7)
  
# add outline
venn2_circles(subsets=(50,10,7)) 
plt.show()

输出:

示例 3:

我们还可以用虚线样式和线宽自定义圆的轮廓:

蟒蛇3

# import modules
from matplotlib_venn import venn2, venn2_circles
from matplotlib import pyplot as plt
  
# depict venn diagram
venn2(subsets=(50, 10, 7), 
      set_labels=('Group A', 'Group B'),
      set_colors=("orange", "blue"), alpha=0.7)
  
# outline of the circle with defined 
# line style and line width
venn2_circles(subsets=(50, 10, 7), 
              linestyle="dashed", linewidth=2)
plt.show()

输出:

示例 4:

可以使用title()方法为维恩图分配标题



蟒蛇3

# import modules
from matplotlib_venn import venn2, venn2_circles
from matplotlib import pyplot as plt
  
# depict venn diagram
venn2(subsets=(50, 10, 7), 
      set_labels=('Group A', 'Group B'),
      set_colors=("orange", "blue"), alpha=0.7)
  
# add outline
venn2_circles(subsets=(50, 10, 7), 
              linestyle="dashed", 
              linewidth=2)
  
# assign title of the venn diagram
plt.title("Venn Diagram in geeks for geeks")  
plt.show()

输出:

示例 6:

让我们使用venn3venn3_circles模块绘制三个维恩图。

蟒蛇3

# import module
from matplotlib_venn import venn3, venn3_circles
from matplotlib import pyplot as plt
  
# depict venn diagram
venn3(subsets=(20, 10, 12, 10, 9, 4, 3), 
      set_labels=('Group A', 'Group B', 'Group C'), 
      set_colors=("orange", "blue", "red"), alpha=0.7)
  
# outline of circle line style and width
venn3_circles(subsets=(20, 10, 12, 10, 9, 4, 3),
              linestyle="dashed", linewidth=2)
  
# title of the venn diagram
plt.title("Venn Diagram in geeks for geeks")
plt.show()

输出:

示例 7:

让我们使用get_patch_by_id()方法自定义图表每个区域的颜色。

蟒蛇3

#import module
from matplotlib_venn import venn3, venn3_circles
from matplotlib import pyplot as plt
  
# depict venn diagram
v = venn3(subsets=(1, 1, 1, 1, 1, 1, 1), 
          set_labels=('A', 'B', 'C'))
  
# set color to defined path id
v.get_patch_by_id("100").set_color("white")
# set text to defined label id
v.get_label_by_id("100").set_text("unknown")
# set text to defined label id "A"
v.get_label_by_id('A').set_text('A new')
  
# add outline
venn3_circles(subsets=(1, 1, 1, 1, 1, 1, 1), 
              linestyle="dashed", linewidth=2)
  
# assign title
plt.title("Venn Diagram in geeks for geeks")
plt.show()

输出: