python matplotlib_venn venn2_unweighted_circles needed

1.1k Views Asked by At

enter code hereI am trying to create venn diagram using matplotlib_venn. combination of venn2 and venn2_circle produces nice diagram with border. Now trying to make unweighted version using venn2_unweighted but could not find venn2_unweighted_circle or any alternative to make the border. any help?

from matplotlib_venn import venn2, venn2_circles, venn2_unweighted
def compare_items(items1, items2, labels = ['Set A', 'Set B'], venn = False,title='',colors=None,
                  border=True, fill=True, unweighted = True):
    list1 = set(items1)
    list2 = set(items2)
    common = list(list1.intersection(list2))
    list1_only = list1.difference(common)
    list2_only = list2.difference(common)
    subsets = (len(list1_only), len(list2_only), len(common))
    print('{label1} = {count1} \t {label2} = {count2}\t Common = {count_com}'.format(
          label1 =labels[0],count1 = len(list1), label2 = labels[1], count2 =len(list2),count_com = len(common)))
    if venn:
        fig = plt.figure(figsize=(8,6))
        color_used = colors if colors else ("orange", "lightblue")

        if fill:
            if unweighted:
                venn2_unweighted(subsets=subsets, set_labels=labels, set_colors=color_used, alpha=0.7,ax=plt.gca())
            else:
                venn2(subsets=subsets, set_labels=labels, set_colors=color_used, alpha=0.7,ax=plt.gca())
        if border:
            border_color = ['#ffffff']*2
            if unweighted:
                venn2_unweighted_circles(subsets=subsets,ax=plt.gca())
            else:
                circles = venn2_circles(subsets=subsets,ax=plt.gca()) 
            for circle, color in zip(circles, border_color):
                circle.set_lw(2.0)
                circle.set_ls('solid')
                circle.set_alpha(1)
                circle.set_edgecolor(color)
                
        plt.gca().set_title(title)
1

There are 1 best solutions below

0
On

While venn2_unweighted_circles does not exist, you can simply use venn2_circles with subsets = (1, 1, 1).

import matplotlib.pyplot as plt
from matplotlib_venn import venn2_unweighted
from matplotlib_venn import venn2_circles

v = venn2_unweighted(subsets = (12, 54, 938), set_labels = ('Set1', 'Set2'))

c = venn2_circles(subsets = (1, 1, 1))

Alternatively, you could use venn2 with subsets = (1, 1, 1), but change the text labels to the numbers you want to display:

v = venn2(subsets = (1, 1, 1), set_labels = ('Set1', 'Set2'))

v.get_label_by_id("10").set_text("12")
v.get_label_by_id("01").set_text("54")
v.get_label_by_id("11").set_text("938")

c = venn2_circles(subsets = (1, 1, 1))