plot actual set items in python, not the number of items

后端 未结 2 1431
清酒与你
清酒与你 2021-01-15 05:11

I wrote this small function:

def sets():
    set1 = random.sample(range(1, 50), 10)
    set2 = random.sample(range(1, 50), 10)
    return(set1,set2)

sets()
         


        
相关标签:
2条回答
  • 2021-01-15 06:00

    A possible solution is to output the labels instead of the set size. With the matplotlib_venn package, you can do something like this:

    import matplotlib.pyplot as plt
    from matplotlib_venn import venn2
    import random
    
    set1 = set(random.sample(range(1, 50), 10))
    set2 = set(random.sample(range(1, 50), 10))
    venn = venn2([set1,set2], ('Group A', 'Group B'))
    
    venn.get_label_by_id('100').set_text('\n'.join(map(str,set1-set2)))
    venn.get_label_by_id('110').set_text('\n'.join(map(str,set1&set2)))
    venn.get_label_by_id('010').set_text('\n'.join(map(str,set2-set1)))
    plt.axis('on')
    plt.show()
    

    We're accessing the labels by a binary ID, which denotes the sets.

    0 讨论(0)
  • 2021-01-15 06:11

    The default behaviour of the venn2 package is to print the size of the overlap of the two sets. Here's the line of the source code where those sizes are added to the Venn diagram plot: https://github.com/konstantint/matplotlib-venn/blob/master/matplotlib_venn/_venn2.py#L247

    To make this print the overlapping numbers you'll have to change the compute_venn2_subsets(a,b) function in this file. Replace the returned argument of compute_venn2_subsets(a,b) with:

    ([val for val in a if val not in b], [val for val in a if val in b], [val for val in b if val not in a])
    

    instead of the set sizes that it's returning right now. If you only want to print the overlapping columns, then make compute_venn2_subsets(a,b) return

    ("", [val for val in a if val in b], "")
    
    0 讨论(0)
提交回复
热议问题