By default matplotlib-venn
's venn3
prints counts in each overlap. How do I print percentages instead?
Asked
Active
Viewed 601 times
1

Harm
- 590
- 3
- 21
2 Answers
1
I figured it out. Just divide each label by the total:
from matplotlib_venn import venn3
subsets = (1, 1, 1, 2, 1, 2, 2)
total = sum(subsets)
venn = venn3(subsets=subsets, set_labels=('Set1', 'Set2', 'Set3'))
for id in [''.join(map(str, i)) for i in product([0, 1], [0, 1], [0, 1])]:
if id == '000':
continue
count = int(venn.get_label_by_id(id).get_text())
venn.get_label_by_id(id).set_text('{:.1%}'.format(count / total))

Harm
- 590
- 3
- 21
1
A cleaner way is to use the subset_label_formatter
argument, as described in this answer: https://stackoverflow.com/a/53490475/4133418
It would look something like this:
venn = venn3(subsets=subsets, set_labels=('Set1', 'Set2', 'Set3'),
subset_label_formatter=lambda x: f"{(x/total):1.0%})

Joe Fradley
- 31
- 3