Python 中的桑基图

问题描述 投票:0回答:6

是否有用于生成 Sankey 图 的 Python 库?

我看过这个 Sankey 图应用程序和库列表,但它们都不是用 Python 编写的。

python graph visualization diagram sankey-diagram
6个回答
10
投票

显然 matplotlib 1.1 现在可以做到这一点。 代码和示例输出在这里.

下面是展示它能做什么的截图。

enter image description here


3
投票

您可以使用 plotly dash 中的 sankey 图

一个基本的说明性示例,改编来自上面的链接:

import plotly.graph_objects as go

fig = go.Figure(data=[go.Sankey(
    node = dict(
      pad = 15,
      thickness = 20,
      line = dict(color = "black", width = 0.5),
      label = ["A1", "A2", "B1", "B2", "C1", "C2"],
      color = "green"
    ),
    link = dict(
      source = [0, 1, 0, 2, 3, 3], # indices correspond to labels, eg A1, A2, A1, B1, ...
      target = [2, 3, 3, 4, 4, 5],
      value = [7, 5, 3, 9, 5, 3]
  ))])

fig.update_layout(title_text="Simple Sankey Diagram using plotly in Python.", font_size=10)
fig.show()

2
投票

我在谷歌上搜索了“Python 图形可视化”并找到了一些东西。有几个库具有“弹簧”行为,软件可以平衡图形并使其美观;他们为你做了很多工作。但他们画的是图表,有节点和边,一点也不像桑基图。

谷歌搜索“Python sankey”没有产生有用的结果。

我在“Python 矢量图形”上做了一些谷歌搜索,发现这个非常有希望的结果:

http://pypi.python.org/pypi/Things

对于 Things,显然您在 Inkscape(一个免费的矢量编辑程序)中绘制基本形状,然后您编写 Python 来缩放、旋转等形状以生成图像或动画。应该可以编写 Python 代码来自动使 Sankey 箭头从它们的源头弹开,向上,向下,等等。

另请参阅 StackOverflow 中关于用 Python 绘制矢量图的讨论:svg diagrams using python

那个页面把我带到了:http://caigraphics.org/ 看起来很有用。

您链接的博客有一篇关于自动创建桑基图的文章:

http://www.sankey-diagrams.com/sankey-diagrams-are-directed-weighted-graphs/

http://www.svgopen.org/2003/papers/RenderingGraphs/index.html

我没有找到适合您的交钥匙解决方案,但我确实找到了一些可能在解决方案中使用的部分。祝你好运。


2
投票

在我的研究小组中,我们在 Jupyter notebooks 中使用来自 Python 的 Sankey 图,使用开源项目(注意:我是这些项目的开发人员)在输出中嵌入基于 D3/SVG 的 Sankey。

  • floWeaver 为绘制桑基图时经常涉及的数据聚合提供更多结构,
  • ipysankeywidget 只是绘制桑基图。

这两个都基于 d3 库,d3-sankey-diagram,它为标准的 d3 sankey 插件添加了一些特性(循环、多种流类型、对布局的更多控制)。


0
投票

其他答案提供了很多不错的选择。然而,我想要一些与 Matplotlib 一起工作的东西,但看起来更像是你从 sankeymatic.

得到的图表

我找到了pySankey,但它只允许 1 级双射流。

我结束了写一个包SankeyFlow。这纯粹使用 Matplotlib 并产生如下所示的流程。

from sankeyflow import Sankey
import matplotlib.pyplot as plt

plt.figure(figsize=(20, 10), dpi=144)

flows = [
    ('Product', 'Total revenue', 20779),
    ('Sevice\nand other', 'Total revenue', 30949),
    ('Total revenue', 'Gross margin', 34768),
    ('Total revenue', 'Cost of revenue', 16960),
    ('Gross margin', 'Operating income', 22247),
    ('Operating income', 'Income before\nincome taxes', 22247, {'flow_color_mode': 'dest'}),
    ('Other income, net', 'Income before\nincome taxes', 268),
    ('Gross margin', 'Research and\ndevelopment', 5758), 
    ('Gross margin', 'Sales and marketing', 5379), 
    ('Gross margin', 'General and\nadministrative', 1384),
    ('Income before\nincome taxes', 'Net income', 18765), 
    ('Income before\nincome taxes', 'Provision for\nincome taxes', 3750),
]

s = Sankey(flows=flows, flow_color_mode='lesser')
s.draw()
plt.show()

0
投票

我找到了一个博客,应该会有帮助:https://medium.com/@yoloshe302/sankey-diagram-for-the-customer-journey-24bccb5f0503,它包括:

  • 随机生成的在线购物数据集
  • 使用 pandas 创建目标和源数据框
  • 通过 Plotly 生成桑基图
© www.soinside.com 2019 - 2024. All rights reserved.