konstantint / matplotlib-venn

Area-weighted venn-diagrams for Python/matplotlib
MIT License
512 stars 68 forks source link

Feature Request: Percentage Labels #63

Closed jcharkow closed 2 years ago

jcharkow commented 3 years ago

Hi,

I was wondering if there was an easy way to change the subset labels so that they were expressed as a percentage of the total number of points.

I was looking at trying to do this using the subset_label_formatter but I could not find a way

konstantint commented 3 years ago

For example like that:

subsets = (1,2,3)
venn2(subsets, subset_label_formatter=lambda v: '{:.2%}'.format(v/sum(subsets)))

or, if you divide by the sum before the plot:

subsets = [v/sum(subsets) for v in subsets]
venn2(subsets, subset_label_formatter='{:.2%}'.format)
jcharkow commented 2 years ago

Thank you for your response and sorry for the delayed reply, this solved my issue! Just in case someone else has this problem, for me my solution was as follows (I am using sets instead of numbers): A = set([1,2,3,4,5]) B = set([2,6,7,9]) C = set([6,3,9,7,10,2]) tmp = A.union(B) totLen = len(tmp.union(C)) venn3([A,B,C], subset_label_formatter=lambda v: f'{v/totLen:.2%}')