目前可找到的资料,关于文氏图的,只能最多画到3个集合的文氏图,没有安装的要先pip install matplotlib-venn,接下来直接上代码了。
import pandas as pd
import numpy as np
#超市
data_chaoshi=pd.read_excel('C:\\Users\\02180085\Desktop\\百货餐饮超市连带消费\\超市会员消费.xlsx')
#商友
data_shangyou=pd.read_excel('C:\\Users\\02180085\Desktop\\百货餐饮超市连带消费\\商友消费明细.xls')
# 百货
data_baihuo=data_shangyou.loc[(data_shangyou['课室']!='超级市场课')|(data_shangyou['课室']!='生活美食课')]
#餐饮
data_canyin=data_shangyou.loc[(data_shangyou['课室']=='生活美食课')|(data_shangyou['部类']=='B2F小型餐厅')|
(data_shangyou['部类']=='小型餐厅')|(data_shangyou['部类']=='美食档口')|
(data_shangyou['部类']=='甜品烘焙')|(data_shangyou['部类']=='休闲食品')]
#非可视化计算交叉情况
print('超市',len(set(data_chaoshi['会员卡号'])))
print('百货',len(set(data_baihuo['会员卡号'])))
print('餐饮',len(set(data_canyin['会员卡号'])))
print('超市&百货',len(set(data_chaoshi['会员卡号'])&set(data_baihuo['会员卡号'])))
print('超市&餐饮',len(set(data_chaoshi['会员卡号'])&set(data_canyin['会员卡号'])))
print('百货&餐饮',len(set(data_canyin['会员卡号'])&set(data_baihuo['会员卡号'])))
print('超市&百货&餐饮',len(set(data_chaoshi['会员卡号'])&set(data_baihuo['会员卡号'])&set(data_canyin['会员卡号'])))
print('仅超市',len(set(data_chaoshi['会员卡号'])-set(data_baihuo['会员卡号'])-set(data_canyin['会员卡号'])))
print('仅百货',len(set(data_baihuo['会员卡号'])-set(data_chaoshi['会员卡号'])-set(data_canyin['会员卡号'])))
print('仅餐饮',len(set(data_canyin['会员卡号'])-set(data_baihuo['会员卡号'])-set(data_chaoshi['会员卡号'])))
#开始绘制文氏图
import matplotlib.pyplot as plt
# 设置中文显示
plt.rcParams['font.sans-serif']='SimHei'
plt.rcParams['axes.unicode_minus']=False
# 导入库,注意没有安装的要先pip install matplotlib-venn
from matplotlib_venn import venn3
# sub接受一个set组成的列表,set_labels接受名称列表,其他参数自行去查看啦
venn3(subsets=[set(data_chaoshi['会员卡号']),set(data_baihuo['会员卡号']),set(data_canyin['会员卡号'])],set_labels=['超市','百货','餐饮'],)
plt.show()
'''
# 如果是画两个集合的韦恩图,就以下代码,其他不变
from matplotlib_venn import venn2, venn2_circles
venn2(subsets=[set(data_chaoshi['会员卡号']),set(data_baihuo['会员卡号'])])
plt.show()
'''