【问题标题】:How do I print percentages in matplotlib-venn diagrams?如何在 matplotlib-venn 图中打印百分比?
【发布时间】:2022-06-18 03:30:12
【问题描述】:

默认情况下,matplotlib-vennvenn3 在每个重叠中打印计数。如何改为打印百分比?

【问题讨论】:

    标签: matplotlib-venn


    【解决方案1】:

    我想通了。只需将每个标签除以总数即可:

    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))
    

    【讨论】:

      【解决方案2】:

      更简洁的方法是使用 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%})
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-15
        • 2014-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多