更改线型圆段维恩图

Change line style circle segment Venn diagram

我有这个维恩图:

from matplotlib_venn import venn2, venn2_circles

venn2(subsets = (30, 10, 5), set_labels = ('Group A', 'Group B'), set_colors=('r', 'g'), alpha = 0.5);
venn2_circles(subsets = (30, 10, 5));

是否可以将 A 组和 B 组重叠的圆段(即棕色区域周围的线)的线条样式更改为虚线?

选项 1

通过在维恩图(ABC)中的每个补丁上设置edgecolor,我们可以看到路径重叠重叠部分。

import matplotlib.pyplot as plt
from matplotlib_venn import venn2, venn2_circles

for char in "ABC":
    v = venn2(
        subsets=(30, 10, 5),
        set_labels=("Group A", "Group B"),
        set_colors=("r", "g"),
        alpha=0.5,
    )
    overlap = v.get_patch_by_id(char)
    overlap.set_edgecolor("black")
    overlap.set_alpha(1)
    overlap.set_ls("dashed")
    overlap.set_lw(1.5)
    plt.show()
A B C

但是,通过使用对比色,我们可以勾勒出重叠部分。这种方法的缺点是我们松开了重叠部分的 alpha

# plot venn
v = venn2(
    subsets=(30, 10, 5),
    set_labels=("Group A", "Group B"),
    set_colors=("r", "g"),
    alpha=0.5,
)
# add circles
c = venn2_circles(subsets=(30, 10, 5), linewidth=2, color="black")

overlap = v.get_patch_by_id("C")  # get the overlap
overlap.set_edgecolor("white")    # set the edgecolor
overlap.set_alpha(1)              # set the alpha to 1 otherwise 
                                  #   we can't see the line
overlap.set_linestyle("dashed")   # set a dashed line
overlap.set_linewidth(2)          # set same linewidth as the circles
overlap.set_zorder(2)             # bump overlap up a level so we can 
                                  #   see the line


选项 2

除此问题外,此方法完全未经测试据我所知,它应该适用于大多数 2 圆维恩图。

这个有点复杂,但它允许我们在重叠部分周围无缝添加虚线而不影响 alpha。 它要求我们从 matplotlib_venncircle_circle_intersection 中的私有模块导入一个函数,我们将使用它来创建 matplotlib.patches.Arcs.

这些圆弧将替换 matplotlib_venn 绘制的圆。

import math
import matplotlib.pyplot as plt
from matplotlib.patches import Arc
from matplotlib_venn._math import circle_circle_intersection

# Create `v` and `c` as in the previous example

# Get the current ax
ax = plt.gca()
# Get the intersections of the circles
(P2x, P2y), (P3x, P3y) = circle_circle_intersection(
    c[0].center, c[0].radius, c[1].center, c[1].radius
)

# Iterate through the circles matplotlib_venn added
for _c in c:
    # remove the circle
    _c.set_visible(False)
    # get the center or the circle and the angle at the center (theta)
    P1x, P1y = _c.center
    theta = math.degrees(
        math.atan2(P3y - P1y, P3x - P1x) - math.atan2(P2y - P1y, P2x - P1x)
    )
    # Add the Arcs
    ax.add_patch(
        Arc(
            xy=_c.center,
            width=_c.radius * 2,
            height=_c.radius * 2,
            linewidth=2,
            color="black",
            angle=theta / 2 if theta < 180 else theta / -2,
            theta2=360 - theta if theta < 180 else theta,
        )
    )
    ax.add_patch(
        Arc(
            xy=_c.center,
            width=_c.radius * 2,
            height=_c.radius * 2,
            linewidth=2,
            color="black",
            linestyle="dashed",
            angle=theta / 2 if theta > 180 else theta / -2,
            theta2=360 - theta if theta > 180 else theta,
        )
    )
plt.show()

产生: