Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

The output of this function looks like this:

([24, 29, 43, 42, 45, 28, 26, 3, 8, 21],
 [22, 37, 38, 44, 25, 42, 29, 7, 35, 9])

I want to plot this in a two way Venn diagram. I know how to plot the NUMBERS of overlap between the sets using the matplotlib, i.e. using this exact code; however I want to plot the ACTUAL VALUES in the plot instead.

i.e. the overlap between the two should read: 29,42 as these are the two items in common, and not the number 2, to represent the number of numbers that overlap.

Would someone know how to do this?

like image 901
Slowat_Kela Avatar asked Mar 19 '26 06:03

Slowat_Kela


1 Answers

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. enter image description here

like image 108
Gabe H. Avatar answered Mar 20 '26 19:03

Gabe H.