Skip to main content

使用 Pandas 和 Matplotlib 绘制 Lex 等人的 UpSet 图

项目描述

PyPi 上的最新版本 执照 支持的 Python 版本

问题跟踪器 Travis CI 构建状态 文件状态 测试覆盖率

这是 Lex 等人的 UpSet 图的另一个 Python 实现。[Lex2014]。UpSet 图用于可视化集合重叠;像维恩图,但更具可读性。文档位于https://upsetplot.readthedocs.io

这个upstplot库试图提供一个简单的接口,支持一个可扩展的、面向对象的设计。

如我们的数据格式指南中所述,有许多方法可以表示数据的分类。

我们的内部输入格式使用pandas.Series包含对应于子集大小的计数,其中每个子集是命名类别的交集。系列的索引通过具有多个布尔索引来指示哪些行属于哪些类别,如下例所示

>>> from upsetplot import generate_counts
>>> example = generate_counts()
>>> example
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

然后:

>>> from upsetplot import plot
>>> plot(example)  # doctest: +SKIP
>>> from matplotlib import pyplot
>>> pyplot.show()  # doctest: +SKIP

使:

http://upsetplot.readthedocs.io/en/latest/_images/sphx_glr_plot_generated_001.png

该图显示了我们数据中看到的每个类别组合的基数。最左边的列计算任何类别中不存在的项目。接下来的三列分别只计算cat1cat2cat3中的项目,以下列显示恰好两个命名集的每个组合中项目的基数。最右边的列计算所有三组中的项目。

回转

我们将上述绘图样式称为“水平”,因为类别交叉点是从左到右呈现的。 还支持垂直图!

http://upsetplot.readthedocs.io/en/latest/_images/sphx_glr_plot_vertical_001.png

分布

提供一个 DataFrame 而不是一个 Series 作为输入,我们可以表达地 绘制 每个子集中变量的分布。

http://upsetplot.readthedocs.io/en/latest/_images/sphx_glr_plot_boston_001.png

加载数据集

虽然上面的数据集是随机生成的,但您可以准备自己的数据集以输入到 uptplot 中。一个有用的工具是from_memberships,它允许我们通过指示每个数据点的类别成员来重构上面的示例:

>>> from upsetplot import from_memberships
>>> example = from_memberships(
...     [[],
...      ['cat2'],
...      ['cat1'],
...      ['cat1', 'cat2'],
...      ['cat0'],
...      ['cat0', 'cat2'],
...      ['cat0', 'cat1'],
...      ['cat0', 'cat1', 'cat2'],
...      ],
...      data=[56, 283, 1279, 5882, 24, 90, 429, 1957]
... )
>>> example
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
dtype: int64

另请参见from_contents,另一种描述分类数据的方式,以及 from_indicators,它允许每个类别由数据框中的列(或列数据的函数,例如是否为缺失值)指示。

安装

要安装库,您可以使用pip

$ pip install upsetplot

安装要求:

  • 熊猫

  • matplotlib >= 2.0

  • seaborn 使用UpSet.add_catplot

然后应该可以:

>>> import upsetplot

在 Python 中。

为什么要替代 py-upset?

可能是因为一些小事。看起来py-upset没有得到维护。它的输入格式没有记录,效率低下,而且 IMO 不合适。它不利于显示每个子集的分布图,就像 Lex 等人在介绍 UpSet 图的工作中那样。它也不包括那里所示的水平条形图。它不支持 Python 2。我认为构建一个更干净的版本比修复它更容易。

参考

[ Lex2014 ]

Alexander Lex, Nils Gehlenborg, Hendrik Strobelt, Romain Vuillemot, Hanspeter Pfister, UpSet: Visualization of Intersecting Sets , IEEE Transactions on Visualization and Computer Graphics (InfoVis '14), vol. 20,没有。12, pp. 1983–1992, 2014. doi: doi.org/10.1109/TVCG.2014.2346248

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

UpSetPlot-0.6.1.tar.gz (18.5 kB 查看哈希

已上传 source