9

使用python包如何以正确的方式制作维恩图

 3 years ago
source link: https://www.bobobk.com/523.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

使用python包如何以正确的方式制作维恩图

2020年7月8日
| 技术

韦恩图在图形可视化方面占据重要地位i,。 在两个集合的韦恩图中,首先,可以有两个(或更多)重叠的圆圈,分别代表不同大小的集合,但是圆圈的大小相同。实际上,圆圈应与集合的大小成比例,重叠区域也应与数据的重叠成比例。这样,在注意到数字之前,您可以立即看到重叠的数字。 这篇文章将详细介绍如何使用Venn函数(matplotlib_venn)在Python中制作一个简单又好看的Venn。 包括两个列表和三个列表的韦恩图。 重要的函数venn2,venn2_circles,一种是直接传入list就行,venn2_circles需要自行计算比较麻烦,因此本文就介绍venn2的绘图。

  • 导入必要的包
  • 准备绘图数据
  • 调用函数绘图

导入必要的包

如果本地没有包的话,使用pip安装即可

pip install matplotlib_venn

直接上代码

import pandas as pd
import numpy as np
import matplotlib as mpl
from matplotlib_venn import venn3, venn3_circles
from matplotlib_venn import venn2, venn2_circles



准备绘图数据

这里选择100以内的随机产生数据获得韦恩图的列表数据。


list1 = np.random.randint(100, size=50)
list1
#array([ 6, 37, 81, 70, 38, 94,  2, 36, 74, 56, 25, 82, 96, 58, 16, 86, 40,
#       95, 38,  4, 23, 58, 39,  0, 38, 78, 73, 26, 81,  5,  1, 12, 99, 86,
#        0, 71,  0, 58, 26, 19, 82, 71,  5, 29,  1, 61, 28,  6, 85, 87])


list2 = np.random.randint(100, size=50)
list2
#array([91,  3,  6, 98, 91, 54,  8, 72, 10, 57, 84,  4, 87, 65,  7, 72, 43,
#       48, 88, 42, 21, 87, 18, 19, 78, 99, 32, 23, 87, 47, 56, 47, 34, 96,
#       99, 17, 96, 96, 18, 68,  2, 15, 41, 71, 97, 51, 69, 36, 74, 28])

list3 = np.random.randint(100, size=50)
list3
#array([81, 55, 72,  6, 16, 20,  3, 48, 76, 76, 58, 65, 82, 23, 24, 72, 54,
#       80, 21, 54, 19, 95, 54,  3, 98, 25, 41, 14, 73, 71, 38, 49, 22, 97,
#       47, 31, 46, 98,  5, 14, 76, 16, 92, 20, 82,  9, 56, 37, 98,  6])

调用函数绘图

开始绘制韦恩图,一个韦恩图是二个list之间的,list1和list2。一个是3个list之间,list1,list2,list3 该函数输入值为一个包含要回值韦恩图的list的数组,label可以自行指定

## 首先是两个list的韦恩图

venn2([set(list1), set(list2),set_labels = ('list1', 'list2'))

venn3([set(list1), set(list2),set(list3)],set_labels = ('list1', 'list2',"list3"))

第一个韦恩图

venn2
第二个韦恩图
venn3

本文主要介绍一个很方便的绘制韦恩图的包matplotlib_venn,其中的venn2和venn3绘图函数实在是很方便。更多函数用法,请查看官方文档 matplotlib-venn


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK