我如何挑选(选择)维恩图区域

时间:2016-05-21 06:59:50

标签: python-3.x matplotlib venn-diagram matplotlib-venn

点击python可以选择或选择维恩图区域吗?

from matplotlib import pyplot as plt
import numpy as np
from matplotlib_venn import venn3, venn3_circles
plt.figure(figsize=(4,4))
v = venn3(subsets=(1, 2, 3, 4, 5, 6, 7), set_labels = ('A', 'B', 'C'))
c = venn3_circles(subsets=(1, 2, 3, 4, 5, 6, 7), linestyle='dashed')
plt.title("Sample Venn diagram")
plt.show()

1 个答案:

答案 0 :(得分:0)

Matplotlib确实支持some degree of event handling以及"选择"绘图组件的事件(无论是维恩图还是任何其他类型的图)。

venn3函数返回的维恩图对象包含一个字段patches,其中列出了构成该图的所有PathPatch个对象。你可以把那些" pickable"与任何其他Matplotlib补丁对象一样:

from matplotlib import pyplot as plt
import numpy as np
from matplotlib_venn import venn3

# Create the diagram
plt.figure(figsize=(4,4))
v = venn3(subsets=(1, 2, 3, 4, 5, 6, 7), set_labels = ('A', 'B', 'C'))
plt.title("Sample Venn diagram")

# Make all patches of the diagram pickable
for p in v.patches:
    if p is not None: p.set_picker(True)

# This is the event handler
def on_pick(event):
    p = event.artist
    ec = p.get_edgecolor()
    p.set_edgecolor('black' if ec[-1] == 0.0 else 'none')
    plt.gcf().canvas.draw()   # Redraw plot

# Connect event handler
plt.gcf().canvas.mpl_connect('pick_event', on_pick)

# Show the plot
plt.show()

venn3_circles函数返回三个Circle补丁的列表,这些补丁是在"正确分段的"上绘制的。图。你也可以选择他们,但是你需要处理"选择冲突"不知何故。因此,一般情况下我建议你使用venn3(如果你需要7个彩色补丁)或venn3_circles(如果你只需要三个圆圈),而不是两者都使用。