2017-09-14 106 views
1

我正在使用matplotlib-venn包在python中绘制维恩图。这个软件包很好地用于绘制两到三组维恩图。但是,当其中一组比其他组更大时,小圈中的计数可以接近或重叠。这是一个例子。Matplotlib带有图例的维恩图

from collections import Counter 
import matplotlib.pyplot as plt 
from matplotlib_venn import venn2, venn3 

sets = Counter() 
sets['01'] = 3000 
sets['11'] = 3 
sets['10'] = 5 
setLabels = ['set1', 'set2'] 

plt.figure() 
ax = plt.gca() 
v = venn2(subsets = sets, set_labels = setLabels, ax = ax) 
plt.title('Venn Diagram') 
plt.show() 

enter image description here

什么我希望做的是移动计数(在这种情况下,3000,3,和5)与符合这些图中色彩的传奇人物。不知道如何用matplotlib_venn来做到这一点。

回答

0

可以更换标签为空字符串的维恩图,而是创建一个从维恩的补丁和相应的数个传说如下:

from collections import Counter 
import matplotlib.pyplot as plt 
from matplotlib_venn import venn2, venn3 

sets = Counter() 
sets['01'] = 3000 
sets['11'] = 3 
sets['10'] = 5 
setLabels = ['set1', 'set2'] 

plt.figure() 
ax = plt.gca() 
v = venn2(subsets = sets, set_labels = setLabels, ax = ax) 

h, l = [],[] 
for i in sets: 
    # remove label by setting them to empty string: 
    v.get_label_by_id(i).set_text("") 
    # append patch to handles list 
    h.append(v.get_patch_by_id(i)) 
    # append count to labels list 
    l.append(sets[i]) 

#create legend from handles and labels  
ax.legend(handles=h, labels=l, title="counts") 

plt.title('Venn Diagram') 
plt.show() 

enter image description here