目录
- 本文速览
- 1、matplotlib_venn
- (1)2组数据venn图
- (2)3组数据venn图
- 2、pyvenn
- 2组数据venn
- 3组数据venn
- 4组数据venn
- 5组数据venn
- 6组数据venn
本文速览
2组数据venn
3组数据venn
4组数据venn
5组数据venn图
6组数据venn
python中Matplotlib并没有现成的函数可直接绘制venn图, 不过已经有前辈基于matplotlib.patches及matplotlib.path开发了两个轮子:
matplotlib_venn【2~3组数据,比较多博客介绍】:https://github.com/konstantint/matplotlib-venn
pyvenn【2~6组数据】:https://github.com/tctianchi/pyvenn
1、 matplotlib_venn
该模块包含'venn2', 'venn2_circles', 'venn3', 'venn3_circles'四个关键函数,这里主要详细介绍'venn2','venn3'同理。
(1)2组数据venn图
matplotlib_venn.venn2(subsets, set_labels=('A', 'B'), set_colors=('r', 'g'), alpha=0.4, normalize_to=1.0XHeyMymMnN, ax=None, subset_label_formatter=None)
绘图数据格式
subsets参数接收绘图数据集,以下5种方式均可以,注意细微异同。
#导入依赖packages import matplotlib.pyplot as plt from matplotlib_venn import venn2,venn2_circles#记得安装matplotlib_venn(pip install matplotlib_venn 或者conda install matplotlib_venn) # subsets参数 #绘图数据的格式,以下5种方式均可以,注意异同 subset = [[{1,2,3},{1,2,4}],#列表list(集合1,集合2) ({1,2,3},{1,2,4}),#元组tuple(集合1,集合2) {'10': 1, '01': 1, '11': 2},#字典dict(A独有,B独有,AB共有) (3, 3, 2),####元组tuple(A有,B有,AB共有),注意和其它几种方式的异同点 [3,3,2]#列表list(A有,B有,AB共有) ] for i in subset: my_dpi=100 plt.figure(figsize=(500/my_dpi, 500/my_dpi), dpi=my_dpi) g=venn2(subsets=i)#默认数据绘制venn图,只需传入绘图数据 plt.title('subsets=%s'%str(i)) plt.show()
一些简单参数介绍
my_dpi=150 plt.figure(figsize=(580/my_dpi, 580/my_dpi), dpi=my_dpi)#控制图尺寸的同时,使图高分辨率(高清)显示 g=venn2(subsets = [{1,2,3},{1,2,4}], #绘图数据集 set_labels = ('Label 1', 'Label 2'), #设置组名 set_colors=("#098154","#c72e29"),#设置圈的颜色,中间颜http://www.cppcns.com色不能修改 alpha=0.6,#透明度 normalize_to=1.0,#venn图占据figure的比例,1.0为占满 ) plt.show()
所有圈外框属性设置
my_dpi=150 plt.figure(figsize=(580/my_dpi, 580/my_dpi), dpi=my_dpi) g=venn2(subsets = [{1,2,3},{1,2,4}], set_labels = ('Label 1', 'Label 2'), set_colors=("#098154","#c72e29"), alpha=0.6, normalize_to=1.0, ) g=venn2_circles(subsets = [{1,2,3},{1,2,4}], linestyle='--', linewidth=0.8, color="black"#外框线型、线宽、颜色 ) plt.show()
单个圈特性设置
g.get_patch_by_id('10')返回一个matplotlib.patches.PathPatch对象,有诸多参数可个性化修改 ,详细见matplotlib官网。
my_dpi=150 plt.figure(figsize=(550/my_dpi, 550/my_dpi), dpi=my_dpi) g=venn2(subsets = [{1,2,3},{1,2,4}], set_labels = ('Label 1', 'Label 2'), set_colors=("#098154","#c72e29"), alpha=0.6, normalize_to=1.0, ) g.get_patch_by_id('10').set_edgecolor('red')#左圈外框颜色 g.get_patch_by_id('10').set_linestyle('--')#左圈外框线型 g.get_patch_by_id('10').set_linewidth(2)#左圈外框线宽 g.get_patch_by_id('01').set_edgecolor('green')#右圈外框颜色 g.get_patch_by_id('11').set_edgecolor('blue')#中间圈外框颜色 plt.show()
单个圈文本设置
g.get_label_by_id('10') 返回一个matplotlib.text.Text对象,有诸多参数可个性化修改 ,详细见matplotlib官网。
my_dpi=150 plt.figure(figsize=(600/my_dpi, 600/my_dpi), dpi=my_dpi) g=venn2(subsets = [{1,2,3},{1,2,4}], set_labels = ('Label 1', 'Label 2'), set_colors=("#098154","#c72e29"), alpha=0.6, normalize_to=1.0, ) g.get_label_by_id('10').set_fontfamily('Microsoft YaHei')#左圈中1的字体设置为微软雅黑 g.get_label_by_id('10').set_fontsize(20)#1的大小设置为20 g.get_label_by_id('10').set_color('r')#1的颜色 g.get_label_by_id('10').set_rotation(45)#1的倾斜度
添加额外注释
my_dpi=150 plt.figure(figsize=(580/my_dpi, 580/my_dpi), dpi=my_dpi)#控制图尺寸的同时,使图高分辨率(高清)显示 g=venn2(subsets = [{1,2,3},{1,2,4}], #绘图数据集 set_labels = ('Label 1', 'Label 2'), #设置组名 set_colors=("#098154","#c72e29"),#设置圈的颜色,中间颜色不能修改 alpha=0.6,#透明度 normalize_to=1.0,#venn图占据figure的比例,1.0为占满 ) plt.annotate('I like this green part!', color='#098154', xy=g.get_label_by_id('10').get_position() - np.array([0, 0.05]), xytext=(-80,40), ha='center', textcoords='offset points', bbox=dict(boxstyle='rounwww.cppcns.comd,pad=0.5', fc='#098154', alpha=0.6),#注释文字底纹 arrowprops=dict(arrowstyle='-|>', connectionstyle='arc3,rad=0.5',color='#098154')#箭头属性设置 ) plt.annotate('She like this red part!', color='#c72e29', xy=g.get_label_by_id('01').get_position() + np.array([0, 0.05]), xytext=(80,40), ha='center', textcoords='offset points', bbox=dict(boxstyle='round,pad=0.5', fc='#c72e29', alpha=0.6), arrowprops=dict(arrowstyle='-|>', connectionstyle='arc3,rad=0.5',color='#c72e29') ) plt.annotate('We both dislike this strange part!', color='black', xy=g.get_label_by_id('11').get_position() + np.array([0, 0.05]), xytext=(20,80), ha='center', textcoords='offset points', bbox=dict(boxstyle='round,pad=0.5', fc='grey', alpha=0.6), arrowprops=dict(arrowstyle='-|>', connectionstyle='arc3,rad=-0.5',color='black') ) plt.show()
多子图绘制venn图
fig,axs=plt.subplots(1,3, figsize=(10,8),dpi=150) g=venn2(subsets = [{1,2,3},{1,2,4}], set_labels = ('Label 1', 'Label 2'), set_colors=("#098154","#c72e29"), alpha=0.6, normalize_to=1.0, ax=axs[0],#该参数指定 ) g=venn2(subsets = [{1,2,3,4,5,6},{1,2,4,5,6,7,8}], set_labels = ('Label 3', 'Label 4'), set_colors=("#098154","#c72e29"), alpha=0.6, normalize_to=1.0, ax=axs[1], ) g=venn2(subsets = [{0,1,2,3},{1,2,4}], set_labels = ('Label 5', 'Label 6'), set_colors=("#098154","#c72e29"), alpha=0.6, normalize_to=1.0, ax=axs[2], ) plt.show()
(2)3组数据venn图
matplotlib_venn.venn3(subsets, set_labels=('A', 'B', 'C'), set_colors=('r', 'g', 'b'), alpha=0.4, normalize_to=1.0, ax=None, subset_label_formatter=None)
参数和venn2几乎一样,介绍几个重要参数
基本参数介绍
my_dpi=150 plt.figure(figsize=(600/my_dpi, 600/my_dpi), dpi=my_dpi)#控制图尺寸的同时,使图高分辨率(高清)显示 g=venn3(subsets = [{1,2,3},{1,2,4},{2,6,7}], #传入三组数据 set_labels = ('Label 1', 'Label 2','Label 3'), #设置组名 set_colors=("#01a2d9", "#31A354", "#c72e29"),#设置圈的颜色,中间颜色不能修改 alpha=0.8,#透明度 normalize_to=1.0,#venn图占据figure的比例,1.0为占满 ) plt.show()
个性化设置图中7部分每一部分
(100, 010, 110, 001, 101, 011, 111)分别代替每一小块,那么代替的是那一小块了?
my_dpi=150 plt.figure(figsize=(600/my_dpi, 600/my_dpi), dpi=my_dpi) g=venn3(subsets = [{1,2,3},{1,2,4},{2,6,7}], set_labels = ('Label 1', 'Label 2','Label 3'), set_colors=("#01a2d9", "#31A354", "#c72e29"), alpha=0.8, normalize_to=1.0, ) for i in list('100, 010, 110, 001, 101, 011, 111'.split(', ')): g.get_label_by_id('%s'%i).set_text('%s'%i)#修改每个组分的文本 #然后就可以如同venn2中那样个性化设置了 g.get_label_by_id('110').set_color('red')#1的颜色 g.get_patch_by_id('110'http://www.cppcns.com).set_edgecolor('red') plt.show()
2、pyvenn
同样,该库还是基于matplotlib.patches二次开发;
区别于上文,pyvenn支持2到6组数据;matplotlib_venn更加灵活多变。
pyvenn具有'venn2', 'venn3', 'venn4', 'venn5', 'venn6'五大主要函数,这里主要介绍venn2,其它同理。
2组数据venn
venn.draw_annotate、venn.draw_text、venn.venn2中的fill()参数非常助于个性化设置。
venn2(labels, names=['A', 'B'], **options) import matplotlib.pyplot as plt #添加pyvenn路径 import sys sys.path.append(r'path\pyvenn-master') import venn mycolor=[[0.10588235294117647, 0.6196078431372549, 0.4666666666666667,0.6], [0.9058823529411765www.cppcns.com, 0.1607843137254902, 0.5411764705882353, 0.6]] labels = venn.get_labels([[1,2,3,4,5,6],[1,2,4,5,6,7,8]], fill=['number', 'logic',#开启每个组分代码 'percent'#每个组分的百分比 ], ) fig, ax = venn.venn2(labels, names=list('AB'), dpi=96, colors=mycolor,#传入RPGA色号,直接传hex色号或者RGB会导致重叠部分被覆盖 fontsize=15,#控制组名及中间数字大小 ) plt.style.use('seaborn-whitegrid') ax.set_axis_on()#开启坐标网格线 #ax.set_title('venn2') # 提取plt.annotate部分参数 venn.draw_annotate(fig, ax, x=0.3, y=0.18, #箭头的位置 textx=0.1, texty=0.05, #箭尾的位置 text='Aoligei!', color='r', #注释文本属性 arrowcolor='r',#箭头的颜色等属性 ) #添加文本 venn.draw_text(fig, ax, x=0.25, y=0.2, text='number:logic(percent)', fontsize=12, ha='center', va='center')
3组数据venn
labels = venn.get_labels([range(10), range(5, 15), range(3, 8)], fill=['number', 'logic', 'percent' ] ) fig, ax = venn.venn3(labels, names=list('ABC'),dpi=96) fig.show()
4组数据venn
labels = venn.get_labels([range(10), range(5, 15), range(3, 8), range(8, 17)], fill=['number', 'logic', 'percent' ]) fig, ax = venn.venn4(labels, names=list('ABCD')) fig.show()
5组数据venn
labels = venn.get_labels([range(10), range(5, 15), range(3, 8), range(8, 17), range(10, 20)], fill=['number', 'logic', 'percent' ]) fig, ax = venn.venn5(labels, names=list('ABCDEF')) fig.show()
6组数据venn
labels = venn.get_labels([range(10), range(5, 15), range(3, 8), range(8, 17), range(10, 20), range(13, 25)], fill=['number', 'logic','percent']) fig, ax = venn.venn6(labels, names=list('ABCDEF')) fig.show()
以上就是Python matplotlib可视化之绘制韦恩图的详细内容,更多关于Python matplotlib韦恩图的资料请关注我们其它相关文章!
精彩评论