首页 > 解决方案 > 来自实际集合的 UpSetPlot

问题描述

鉴于我拥有的实际集合,我想使用UpSetPlot,但我找不到任何以这种方式使用它的示例。标准示例是这样的:

from upsetplot import generate_counts, plot
example = generate_counts()
plot(example, orientation='vertical')

生成example的位置Series如下所示。

cat0   cat1   cat2 
False  False  False      56
              True      283
       True   False    1279
              True     5882
True   False  False      24
              True       90
       True   False     429
              True     1957
Name: value, dtype: int64

cat0有没有办法从类别中cat1的实际元素自动生成这种计数结构cat2

标签: pythonplotsetupsetplot

解决方案


在另一个答案中使用@StupidWolf 的提示,这是我自己问题的答案。给定3套

set1 = {0,1,2,3,4,5}
set2 = {3,4,5,6,10}
set3 = {0,5,6,7,8,9}

下面是为这三组绘制扰动图的代码:

import pandas as pd
from upsetplot import plot
set_names = ['set1', 'set2', 'set3']
all_elems = set1.union(set2).union(set3)
df = pd.DataFrame([[e in set1, e in set2, e in set3] for e in all_elems], columns = set_names)
df_up = df.groupby(set_names).size()
plot(df_up, orientation='horizontal')

在此处输入图像描述

这是第 4 行和第 5 行更改,以将上述代码概括为一组列表,例如sets = [set1, set2, set3]

all_elems = list(set().union(*sets))
df = pd.DataFrame([[e in st for st in sets] for e in all_elems], columns = set_names)

推荐阅读