在python中绘制实际设置的项目,而不是项目的数量 [英] plot actual set items in python, not the number of items

查看:51
本文介绍了在python中绘制实际设置的项目,而不是项目的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这个小函数:

def set():set1 = random.sample(range(1, 50), 10)set2 = random.sample(range(1, 50), 10)返回(set1,set2)套()

此函数的输出如下:

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

我想用双向维恩图来绘制它.我知道如何使用 matplotlib 绘制集合之间的重叠数,即使用

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, i.e. using this exact code; however I want to plot the ACTUAL VALUES in the plot instead.

i.e. 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.

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:

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.

这篇关于在python中绘制实际设置的项目,而不是项目的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆