简体   繁体   English

在python中绘制实际设置的项目,而不是项目数

[英]plot actual set items in python, not the number of items

I wrote this small function: 我写了这个小函数:

def sets():
    set1 = random.sample(range(1, 50), 10)
    set2 = random.sample(range(1, 50), 10)
    return(set1,set2)

sets()

The output of this function looks like this: 该函数的输出如下所示:

([24, 29, 43, 42, 45, 28, 26, 3, 8, 21],
 [22, 37, 38, 44, 25, 42, 29, 7, 35, 9])

I want to plot this in a two way Venn diagram. 我想以两种方式绘制维恩图。 I know how to plot the NUMBERS of overlap between the sets using the matplotlib, ie using this exact code; 我知道如何使用matplotlib,即使用确切的代码来绘制集合之间的重叠数。 however I want to plot the ACTUAL VALUES in the plot instead. 但是我想在图中绘制实际值。

ie the overlap between the two should read: 29,42 as these are the two items in common, and not the number 2, to represent the number of numbers that overlap. 也就是说,两者之间的重叠应为:29,42,因为这是两个共同的项目,而不是数字2,代表重叠的数字数量。

Would someone know how to do this? 有人知道该怎么做吗?

A possible solution is to output the labels instead of the set size. 一种可能的解决方案是输出标签而不是设置的尺寸。 With the matplotlib_venn package, you can do something like this: 使用matplotlib_venn包,您可以执行以下操作:

import matplotlib.pyplot as plt
from matplotlib_venn import venn2
import random

set1 = set(random.sample(range(1, 50), 10))
set2 = set(random.sample(range(1, 50), 10))
venn = venn2([set1,set2], ('Group A', 'Group B'))

venn.get_label_by_id('100').set_text('\n'.join(map(str,set1-set2)))
venn.get_label_by_id('110').set_text('\n'.join(map(str,set1&set2)))
venn.get_label_by_id('010').set_text('\n'.join(map(str,set2-set1)))
plt.axis('on')
plt.show()

We're accessing the labels by a binary ID, which denotes the sets. 我们正在通过二进制ID(表示集合)访问标签。 在此处输入图片说明

The default behaviour of the venn2 package is to print the size of the overlap of the two sets. venn2软件包的默认行为是打印两组重叠部分的大小。 Here's the line of the source code where those sizes are added to the Venn diagram plot: https://github.com/konstantint/matplotlib-venn/blob/master/matplotlib_venn/_venn2.py#L247 这是将这些大小添加到维恩图图中的源代码行: https : //github.com/konstantint/matplotlib-venn/blob/master/matplotlib_venn/_venn2.py#L247

To make this print the overlapping numbers you'll have to change the compute_venn2_subsets(a,b) function in this file. 要使此打印重叠的数字,您必须更改此文件中的compute_venn2_subsets(a,b)函数。 Replace the returned argument of compute_venn2_subsets(a,b) with: 将返回的compute_venn2_subsets(a,b)参数替换为:

([val for val in a if val not in b], [val for val in a if val in b], [val for val in b if val not in a])

instead of the set sizes that it's returning right now. 而不是现在返回的设置大小。 If you only want to print the overlapping columns, then make compute_venn2_subsets(a,b) return 如果只想打印重叠的列,则使compute_venn2_subsets(a,b)返回

("", [val for val in a if val in b], "")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM