You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否在matplotlib_venn维恩图中用集合元素标签替代或补充集合大小?

Yes, You Can Add Element Labels to Your Venn Diagram!

Matplotlib_venn doesn’t include built-in support for displaying element labels inside the circles, but we can extend it using basic matplotlib text tools. You can either replace the set size labels with your element lists, or keep both by adjusting their positions to avoid overlap.

Here’s a modified version of your code that adds element labels to each section of the Venn diagram:

import matplotlib.pyplot as plt
import matplotlib_venn as mv
import seaborn

# Set up color palette and a larger figure to avoid text crowding
cols = seaborn.color_palette(['orange', 'darkblue'])
plt.figure(figsize=(12, 10))

# Define your sets
s1 = {'TIMP3', 'COL4A1', 'SERPINE1', 'SPARC', 'FBLN2', 'THBS2', 'SERPINE2', 'COL1A1', 'LTBP2'}
s2 = {'SPARK', 'FBLN1', 'VCAN', 'TIMP1', 'SERPINE2', 'SKIL', 'LTBP2', 'TIMP3', 'THBS2', 'TGFBR1', 'BGN', 'VEGFA', 'COL1A1', 'FBN1', 'FN1', 'COL4A1', 'MMP2', 'MMP14', 'COL1A2', 'PMPEA1', 'FGF2'}

# Draw the base Venn diagram and capture the returned object
venn = mv.venn2_unweighted([s1, s2], ['Adult', 'Senescent'], set_colors=cols)

# Calculate elements for each region
only_adult = s1 - s2
only_senescent = s2 - s1
shared_elements = s1 & s2

# Function to add element labels to a region
def add_element_labels(region_id, elements, fontsize=8):
    label = venn.get_label_by_id(region_id)
    if not label:
        return
    # Get the center coordinates of the region
    x, y = label.get_position()
    # Convert elements to a newline-separated string
    element_text = '\n'.join(elements)
    # Add the text to the plot
    plt.text(x, y, element_text, ha='center', va='center', fontsize=fontsize, color='white')
    # Optional: Keep the set size label by moving it up
    label.set_position((x, y + 0.12))
    label.set_fontsize(10)

# Add labels to each region
add_element_labels('10', only_adult)
add_element_labels('01', only_senescent, fontsize=7)  # Smaller font for larger set
add_element_labels('11', shared_elements)

plt.title('Venn Diagram with Element Labels and Set Sizes')
plt.show()

Key Details:

  • Region IDs: The get_label_by_id() method uses a binary ID system:
    • '10': Elements unique to the first set (Adult)
    • '01': Elements unique to the second set (Senescent)
    • '11': Shared elements (intersection)
  • Text Placement: We use the existing position of the set size label as the center for our element list, then optionally move the size label upward to avoid overlap.
  • Font Adjustments: The font size is tuned based on the number of elements in each region to prevent crowding.
  • Canvas Size: Increasing figsize gives more space for text, making the diagram easier to read.

If you’d prefer to replace the set size labels entirely (instead of keeping both), just remove the lines that adjust the label position and font size in the add_element_labels function.

内容的提问来源于stack exchange,提问作者CiaranWelsh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:44:41