Is it possible to put labels rather than set size inside of a `matplotlib_venn` venn diagram?

1.9k Views Asked by At

For instance, the following code:

import matplotlib.pyplot as plt
import matplotlib_venn as mv
import seaborn

cols = seaborn.color_palette(['orange', 'darkblue'])
plt.figure()

s1 = {'TIMP3', 'COL4A1', 'SERPINE1', 'SPARC', 'FBLN2', 'THBS2', 'SERPINE2', 'COL1A1', 'LTBP2'}
s2 = {'SPARK', 'FBLN1', 'VCAN', 'TIMP1', 'SERPINE2', 'SKIL', 'LTBP2', 'TIMP3', 'THBS2', 'TGFBR1', 'BGN', 'VEGFA', 'COL1A1', 'FBN1', 'FN1', 'COL4A1', 'MMP2', 'MMP14', 'COL1A2', 'PMPEA1', 'FGF2'}

mv.venn2_unweighted([s1, s2], ['Adult', 'Senescent'], set_colors=cols)
plt.show()

produces: enter image description here

But it would be useful for me if the labels of which strings belong to each set were inside the circles instead (or perhaps as well as) the size of each set. Is this possible?

1

There are 1 best solutions below

0
On BEST ANSWER

You can access the labels by an ID, which is a string with the numbers 0 and 1 denoting the sets. E.g. v.get_label_by_id("01") gives you the label of the second circle if v is the venn diagramm in use.

You might hence set the labels below the plot to empty strings and instead set new texts for each of the internal labels, reusing the already present size labels.

import matplotlib.pyplot as plt
import matplotlib_venn as mv
import seaborn

cols = seaborn.color_palette(['orange', 'darkblue'])
plt.figure()

s1 = {'TIMP3', 'COL4A1', 'SERPINE1', 'SPARC', 'FBLN2', 'THBS2', 'SERPINE2', 'COL1A1', 'LTBP2'}
s2 = {'SPARK', 'FBLN1', 'VCAN', 'TIMP1', 'SERPINE2', 'SKIL', 'LTBP2', 'TIMP3', 'THBS2', 'TGFBR1', 'BGN', 'VEGFA', 'COL1A1', 'FBN1', 'FN1', 'COL4A1', 'MMP2', 'MMP14', 'COL1A2', 'PMPEA1', 'FGF2'}

v = mv.venn2_unweighted([s1, s2], ["",""], set_colors=cols)

labels = ['Adult', 'Senescent']

def label_by_id(label, ID):
    num = v.get_label_by_id(ID).get_text() 
    v.get_label_by_id(ID).set_text(label+"\n"+num)

for label, ID in zip(labels, ["10", "01"]):
    label_by_id(label, ID)

plt.show()

enter image description here