[python] 基于matplotlib_venn实现维恩图的绘制

VENN DIAGRAM(维恩图)

代码下载地址
维恩图用于显示所有不同组的有限集合之间的可能的逻辑关系。每一组用一个圆圈表示,圆圈大小代表该组的重要性。组通常是重叠的:重叠的大小表示两个组之间的交集。建议不要使Venn图超过3分组,因为它将变得难以阅读。在python中,根据您拥有的组数,使用matplotlib库的venn2和venn3函数来实现Venn图。该章节主要内容有:

  1. 具有2个分组的基本的维恩图 Venn diagram with 2 groups
  2. 具有3个组的基本维恩图 Venn diagram with 3 groups
  3. 自定义维恩图 Custom Venn diagram
  4. 精致的维恩图 Elaborated Venn diagram
  5. 维恩图背景色自定义 change background colour of Venn diagram

需要安装matplotlib_venn模块:

pip install matplotlib_venn

官方文档:

https://pypi.org/project/matplotlib-venn/

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all" 

1. 具有2个分组的基本的维恩图 Venn diagram with 2 groups

使用matplotlib库制作维恩图的主要方法有2种,但结果相同。第一个步骤是直接给出组及其交叉点的大小。第二个是给定两组值,python将自己计算每组的长度和公共值的数量(它们的交集)。通过venn2计算两个分组。

# library
import matplotlib.pyplot as plt
from matplotlib_venn import venn2

# 第一种方法,10,5为两组的大小,2为两组交叉大小;
# set_labels为组名
venn2(subsets = (10, 5, 2), set_labels = ('Group A', 'Group B'));
# 设置两组ABCD和DEF
venn2([set(['A', 'B', 'C', 'D']), set(['D', 'E', 'F'])]);

png

2. 具有3个组的基本维恩图 Venn diagram with 3 groups

通过venn3计算3个分组

# Import the library
import matplotlib.pyplot as plt
from matplotlib_venn import venn3
 
# Make the diagram
venn3(subsets = (10, 8, 22, 6,9,4,2));

png

# 设置三组ABCD、DEF、ADG
venn3([set(['A', 'B', 'C', 'D']), set(['D', 'E', 'F']), set(['A', 'D', 'G','F'])]);

png

3. 自定义维恩图 Custom Venn diagram

  • Venn上的自定义标签 Custom label on Venn
  • 自定义维恩图上圆的线条 Custom Circles lines on Venn
  • 自定义维恩图上的圆 Custom a circle on Venn
## Venn上的自定义标签 Custom label on Venn
# Import the library
import matplotlib.pyplot as plt
from matplotlib_venn import venn3
from matplotlib_venn import venn3_circles


# Custom text labels: change the label of group A
v=venn3(subsets = (10, 8, 22, 6,9,4,2), set_labels = ('Group A', 'Group B', 'Group C'))
# 单独改变A的标签
v.get_label_by_id('A').set_text('My Favourite group!')

png

## 自定义维恩图上圆的线条 Custom Circles lines on Venn
# Line style: can be 'dashed' or 'dotted' for example
# 设置维恩图
v = venn3(subsets = (10, 8, 22, 6,9,4,2), set_labels = ('Group A', 'Group B', 'Group C'))
# 画圆,linestyle线条类型,linewith线宽,color线条颜色
c = venn3_circles(subsets = (10, 8, 22, 6,9,4,2), linestyle='dashed', linewidth=1, color="grey")

png

## 自定义维恩图上的圆 Custom a circle on Venn
# Change one group only
v=venn3(subsets = (10, 8, 22, 6,9,4,2), set_labels = ('Group A', 'Group B', 'Group C'))
c=venn3_circles(subsets = (10, 8, 22, 6,9,4,2), linestyle='dashed', linewidth=1, color="grey")
# 设置第一个圆的线宽
c[0].set_lw(8.0)
# 设置第一个圆的线形
c[0].set_ls('dotted')
# 设置第一个圆的填充颜色
c[0].set_color('skyblue')
 
# Color
# id号
# 如ABC三个簇,010代表非A和B和非C,100代表A和非B和非C
# 设置透明度
v.get_patch_by_id('011').set_alpha(1.0)
# 设置颜色
v.get_patch_by_id('011').set_color('red')
# 打印id号
#v.id2idx

png

4. 精致的维恩图 Elaborated Venn diagram

# libraries
from matplotlib import pyplot as plt
import numpy as np
from matplotlib_venn import venn3, venn3_circles
 
# Make a Basic Venn
# 画最基础维恩图
v = venn3(subsets=(1, 1, 1, 1, 1, 1, 1), set_labels = ('A', 'B', 'C'))
 
# Custom it
# 设置A和非B和非C颜色为白色,标签为透明
v.get_patch_by_id('100').set_alpha(1.0)
v.get_patch_by_id('100').set_color('white')
v.get_label_by_id('100').set_text('Unknown')
# 设置A的标签为'Set "A"
v.get_label_by_id('A').set_text('Set "A"')

# 设置圆的线形
c = venn3_circles(subsets=(1, 1, 1, 1, 1, 1, 1), linestyle='dashed')
# 设置A的线宽和线形
c[0].set_lw(1.0)
c[0].set_ls('dotted')
 
# Add title and annotation
# 添加标签
plt.title("Sample Venn diagram")
plt.annotate('Unknown set', xy=v.get_label_by_id('100').get_position() - np.array([0, 0.05]), xytext=(-70,-70),
ha='center', textcoords='offset points', bbox=dict(boxstyle='round,pad=0.5', fc='gray', alpha=0.1),
arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0.5',color='gray'));

png

5. 维恩图背景色自定义 change background colour of Venn diagram

# Library
from matplotlib import pyplot as plt
from matplotlib_venn import venn2
 
# Basic Venn
v = venn2( (10, 20, 10), alpha = 1 )
 
# Change Backgroud
plt.gca().set_facecolor('skyblue')
# 显示边框
plt.gca().set_axis_on()

png

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值