【问题标题】:Matplotlib Venn diagram with legend带有图例的 Matplotlib 维恩图
【发布时间】:2017-09-14 16:31:14
【问题描述】:

我正在使用 matplotlib-venn 包在 python 中绘制维恩图。这个包非常适合用两到三组绘制维恩图。但是,当其中一组比其他组大得多时,较小圆圈中的计数可能会接近或重叠。这是一个例子。

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()

我要做的是将计数(在本例中为 3000、3 和 5)移动到颜色与图表中的颜色匹配的图例中。不确定如何使用 matplotlib_venn 执行此操作。

【问题讨论】:

    标签: matplotlib matplotlib-venn


    【解决方案1】:

    您可以用空字符串替换维恩图的标签,而是根据维恩的补丁和相应的计数创建一个图例,如下所示:

    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()
    

    【讨论】:

      猜你喜欢
      • 2013-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-28
      • 1970-01-01
      • 2014-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多