How to label Venn diagrams with percentages of set sizes in python?

6.9k Views Asked by At

I am using the matplotlib_venn.venn2 function to plot four different Venn diagrams in a single figure.

My inputs for the function's subset arguments are sets of strings:

    cse_set_ucb = set(['data science','algorithms', 'machine learning',...])

    ece_set_ucb = set(['circuits', 'signals', 'machine learning',....])

    v_ucb = venn2([cse_set_ucb,ece_set_ucb],set_labels=['CSE','ECE'],set_colors=['r','g'])

and similarly for v_ucla, v_ucsb, and v_ucsd.

Current Result:

enter image description here

However, to make the comparison more intuitive, I wanted to show the percentages inside the Venn diagram rather than the set sizes. Is it possible to do so by some functionality in venn2?

For e.g the first plot would be replaced with:

enter image description here

The only alternative I can think of now is to do the normalization myself somehow and then feed the subset sizes into the venn2 function, rather than the sets, but I was hoping for a smarter way to do this

2

There are 2 best solutions below

2
On BEST ANSWER

There's an argument subset_label_formatter which control labels printed using a function.

total = len(set_a.union(set_b))
v1 = venn2(
    [set_a, set_b],
    set_labels=labels_depts,
    set_colors=['red', 'green'],
    subset_label_formatter=lambda x: f"{(x/total):1.0%}"
)

enter image description here

Ref:

1)Percentages in venn diagrams

2)Add parameter to allow formatting of labels of subset sizes

0
On
## total and percentage
total = len(set_a.union(set_b))
v1 = venn2(
    [set_a, set_b],
    set_labels=labels_depts,
    set_colors=['red', 'green'],
    subset_label_formatter=lambda x: str(x) + "\n(" + f"{(x/total):1.0%}" + ")"

)

enter image description here