【发布时间】:2022-06-18 03:30:12
【问题描述】:
默认情况下,matplotlib-venn 的 venn3 在每个重叠中打印计数。如何改为打印百分比?
【问题讨论】:
标签: matplotlib-venn
默认情况下,matplotlib-venn 的 venn3 在每个重叠中打印计数。如何改为打印百分比?
【问题讨论】:
标签: matplotlib-venn
我想通了。只需将每个标签除以总数即可:
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))
【讨论】:
更简洁的方法是使用 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%})
【讨论】: