Search code examples
python-3.xmatplotlib-venn

Is it possible to display the venn diagram within a universal set?


Is it possible to display the universal set using matplotlib-venn? I'm new to both python and the matplotlib package, so I'm not actually sure what's possible and what's not.

I'm trying to create a venn diagram generator that accepts the values for each circle and then an argument (ex. A intersection B), then highlights only the intersection of the two circles. Basically, this is what I want the output to be.

enter image description here


Solution

  • Python has package matplotlib_venn but it needs some tricks in your case.

    import matplotlib.pyplot as plt
    from matplotlib_venn import venn3
    A = set([9,3,6])
    B = set([2,4,6,8])
    C = set([0,5,1,7])
    v = venn3([A,B,C], ('P', 'Q', 'U'))
    
    v.get_label_by_id('100').set_text('\n'.join(map(str,A-B)))
    v.get_label_by_id('110').set_text('\n'.join(map(str,A&B)))
    v.get_label_by_id('010').set_text('\n'.join(map(str,B-A)))
    v.get_label_by_id('001').set_text('\n'.join(map(str,C)))
    v.get_patch_by_id('001').set_color('white')
    plt.axis('on')
    plt.show()
    

    venn

    You can use plt.annotate for configuring the position of values