更改线型圆线段维恩图

问题描述

我有这个维恩图:

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,5));

enter image description here

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

解决方法

选项 1

通过在维恩图中的每个补丁(edgecolorAB)上设置 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()
A B C
A B C

但是,通过使用对比色,我们可以勾勒出重叠的轮廓。这种方法的缺点是我们失去了重叠的 alpha

# plot venn
v = venn2(
    subsets=(30,)
# add circles
c = venn2_circles(subsets=(30,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

overlapped section with dashed edge


选项 2

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

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

这些弧线将替换由 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,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,linestyle="dashed",angle=theta / 2 if theta > 180 else theta / -2,theta2=360 - theta if theta > 180 else theta,)
    )
plt.show()

产生:

Overlapping venn diagram with different edges and preserved alpha

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...