使用 pandas 和 matplotlib_venn 绘制维恩图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37725099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:21:44  来源:igfitidea点击:

Plot venn diagram with pandas and matplotlib_venn

pythonpandasmatplotlib-venn

提问by HonzaB

I'd like to plot venn diagrams based on my pandas data frame. I understand that matplotlib_vennaccepts sets as input. My dataset contain client id and two other columns with information if the client was in campaign or not.

我想根据我的Pandas数据框绘制维恩图。我知道matplotlib_venn接受集合作为输入。我的数据集包含客户 ID 和其他两列,其中包含客户是否参加活动的信息。

df_dataset = pd.read_csv('...path...',delimiter=',',decimal=',')
campaign_a = df_dataset[(df_dataset['CAM_A'] == 1)] 
campaign_b = df_dataset[(df_dataset['CAM_B'] == 1)]

plt.figure(figsize=(4,4))
set1 = set(campaign_a['CLI_ID'])
set2 = set(campaign_b['CLI_ID'])

venn3([set1, set2], ('Set1', 'Set2'))
plt.show()

However I get an error:

但是我收到一个错误:

File "C:\Python27\Lib\site-packages\matplotlib_venn_venn3.py", line 44, in compute_venn3_areas areas = np.array(np.abs(diagram_areas), float)

TypeError: bad operand type for abs(): 'set'

文件“C:\Python27\Lib\site-packages\matplotlib_venn_venn3.py”,第44行,在compute_venn3_areas area = np.array(np.abs(diagram_areas), float)

类型错误:abs() 的错误操作数类型:'set'

UPDATE

更新

Based on lanS advice, it works now. But for some reasons, the diagrams are not together. But in their documentation, the same code works.

根据 lanS 的建议,它现在可以工作了。但由于某些原因,这些图没有在一起。但是在他们的文档中,相同的代码有效。

plt.figure(figsize=(4,4))

set1 = set(campaign_a['CLI_ID'])
set2 = set(campaign_b['CLI_ID'])
set3 = set(union['CLI_ID'])

venn3([set1, set2, set3], ('A', 'B', 'union'))
plt.show()

enter image description here

在此处输入图片说明

UPDATE 2- solution

更新 2- 解决方案

In the end, the simplest approach seems to be only insert size of each space, not dataset. Inspiration here.

最后,最简单的方法似乎是只插入每个空间的大小,而不是数据集。灵感来了

采纳答案by IanS

I believe you need to pass 3 sets. Based on the code here, if you pass three subsets then they are transformed into a tuple before being passed to compute_venn3_areas, where np.abscan handle them. The case when you pass only 2 sets looks like an unhandled error.

我相信你需要通过3套。根据这里代码,如果您传递三个子集,那么它们在传递给可以处理它们的compute_venn3_areas地方之前会被转换成一个元组np.abs。仅传递 2 组的情况看起来像是未处理的错误。

回答by Ignacio Carvajal

This error is a result of trying to force 2 sets into venn3. You need to import venn2 from the same library.

此错误是试图将 2 个集合强制放入 venn3 的结果。您需要从同一个库中导入 venn2。

from matplotlib_venn import venn2

df_dataset = pd.read_csv('...path...',delimiter=',',decimal=',')
campaign_a = df_dataset[(df_dataset['CAM_A'] == 1)] 
campaign_b = df_dataset[(df_dataset['CAM_B'] == 1)]

plt.figure(figsize=(4,4))
set1 = set(campaign_a['CLI_ID'])
set2 = set(campaign_b['CLI_ID'])

venn2([set1, set2], ('Set1', 'Set2'))
plt.show()

回答by Ankush

Simple way to create venn diagrams for small number of sets. Hope this helps.

为少量集合创建维恩图的简单方法。希望这可以帮助。

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

set1 = set()
set2 = set()
set3 = set()
set4 = set()
set_array = []
set_names = ['Set1', 'Set2', 'Set3', 'Set4']

set1.add('a')
set1.add('b')

set2.add('b')
set2.add('c')

set3.add('c')
set3.add('d')

set4.add('d')
set4.add('e')

set_array.append(set1)
set_array.append(set2)
set_array.append(set3)
set_array.append(set4)

# venn2([set1, set2], ('Set1', 'Set2')) # venn2 works for two sets
venn3(set_array[0:3], set_names[0:3])   # venn3 works for three sets
plt.show()

sample output for above code

上述代码的示例输出