【问题标题】:Change line style circle segment Venn diagram改变线型圆段维恩图
【发布时间】:2021-07-06 10:27:33
【问题描述】:

我有这个维恩图:

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 组重叠的圆段(即棕色区域周围的线)的虚线?

【问题讨论】:

  • 我不太了解matplotlib,但找到了一个在线资源,其中显示了一个示例。 The resource is here.当您解决了您的问题后,请记得发布解决方案。
  • 我已经更新了我的答案,加入了另一种添加这种线条样式的方法,尽管它很复杂。
  • 非常感谢您花时间给出如此全面的答案

标签: python matplotlib matplotlib-venn


【解决方案1】:

选项 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-circle venn 图。

这个有点复杂,但它允许我们在重叠周围无缝添加虚线,而不会影响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()

产生:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-24
    • 1970-01-01
    • 2022-09-23
    • 2013-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多