默认情况下,matplotlib-venn的venn3打印在每个重叠中都是计数的。如何打印百分比呢?
发布于 2022-01-31 16:05:38
我想通了。只需将每个标签除以总数:
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))发布于 2022-06-17 19:30:12
一种更简洁的方法是使用subset_label_formatter参数,如下面的答案:https://stackoverflow.com/a/53490475/4133418所述
看起来会是这样的:
venn = venn3(subsets=subsets, set_labels=('Set1', 'Set2', 'Set3'),
subset_label_formatter=lambda x: f"{(x/total):1.0%})https://stackoverflow.com/questions/70929066
复制相似问题