python - 来自实际集合的 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
?
解决方案
在另一个答案中使用@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)
推荐阅读
- react-native - CameraRoll.saveToCameraRoll 不是函数(Expo)
- python - 在 NLTK 中查找 n-gram 背后的想法或算法是什么?
- open-policy-agent - 怎么办 || 雷戈中的“b”
- python - 如果标记尚未绘制/圈出那些缺失的标记,则绘制标记
- python - python子进程中的watch命令仅打印输出的变化
- powershell - 在不加载任何配置文件的情况下运行 powershell .ps1 脚本
- laravel - 如何使用 Laravel 将查询插入数据库?
- javascript - ES6/ES7 扩展类同名
- asp.net - 项目构建中的错误 - ASP.NET Core 2.1
- ms-access - 将 PHP 查询转换为 MS Access