选项1
通过edgecolor
在维恩图中的每个补丁上设置 (A
、B
和C
),我们可以看到路径在重叠部分周围重叠。
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()
但是,通过使用对比色,我们可以勾勒出重叠部分。这种方法的缺点是我们失去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
. 它要求我们从 in 的私有模块中导入一个函数matplotlib_venn
,circle_circle_intersection
我们将使用它来创建matplotlib.patches.Arc
s。
这些圆弧将替换 绘制的圆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()
产生:
