You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Plotly Python中桑基图连线穿过节点的原因及解决方法

Plotly桑基图连线穿过节点的问题分析与解决方法

问题现象

使用Plotly Python绘制桑基图时,出现一条对应"Verlies: 1 MW"节点的连线穿过节点的异常情况,其余连线均正常与节点分离,示意图如下:
穿过节点的"Verlies: 1 MW"连线

相关代码如下:

fig = go.Figure(data=[go.Sankey(
    node = dict(
        pad = 100,
        thickness = 30,
        line = dict(color = "black", width = 2),
        label = [" Gas: 47 MW",
                 " Stoom: 35 MW",
                 " Elektriciteit: 12 MW",
                 "Verlies: 1 MW ",
                 "R: 17 MW ",
                 "F: 14 MW ",
                 "Fe: 8.5 MW ",
                 "Ut: 4.5 MW ",
                 "VN: 4.5 MW ",
                 ],
    ),
    link = dict(
        source = [0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2],
        target = [1, 2, 3, 4, 5, 6, 7, 4, 5, 6, 7, 8],
        value = [35, 12, 1, 13.8, 11.3, 7.3, 2.8, 3.4, 2.4, 1.1, 1.5, 3.6],
    )
)])

问题成因

  1. 极小流量节点的布局优先级问题:"Verlies: 1 MW"节点对应的流量仅为1,远小于其他节点的流量。Plotly桑基图的自动布局算法会优先根据流量权重分配节点位置,小流量节点的布局优先级低,容易出现路径计算偏差。
  2. 过大的pad参数干扰:代码中设置了pad = 100,该参数控制节点之间的间距。过大的间距会让布局算法在处理小流量连线时,无法正确计算连线与节点的衔接路径,最终导致连线直接穿过节点。

解决方法

方法1:减小pad参数值

降低节点间的间距,让布局算法能更合理地计算小流量连线的路径:

fig = go.Figure(data=[go.Sankey(
    node = dict(
        pad = 20,  # 减小pad值,降低节点间距
        thickness = 30,
        line = dict(color = "black", width = 2),
        label = [" Gas: 47 MW",
                 " Stoom: 35 MW",
                 " Elektriciteit: 12 MW",
                 "Verlies: 1 MW ",
                 "R: 17 MW ",
                 "F: 14 MW ",
                 "Fe: 8.5 MW ",
                 "Ut: 4.5 MW ",
                 "VN: 4.5 MW ",
                 ],
    ),
    link = dict(
        source = [0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2],
        target = [1, 2, 3, 4, 5, 6, 7, 4, 5, 6, 7, 8],
        value = [35, 12, 1, 13.8, 11.3, 7.3, 2.8, 3.4, 2.4, 1.1, 1.5, 3.6],
    )
)])

方法2:手动指定节点位置

通过node.x和node.y参数固定每个节点的坐标,完全控制布局逻辑,避免自动算法的偏差:

fig = go.Figure(data=[go.Sankey(
    node = dict(
        pad = 100,
        thickness = 30,
        line = dict(color = "black", width = 2),
        label = [" Gas: 47 MW",
                 " Stoom: 35 MW",
                 " Elektriciteit: 12 MW",
                 "Verlies: 1 MW ",
                 "R: 17 MW ",
                 "F: 14 MW ",
                 "Fe: 8.5 MW ",
                 "Ut: 4.5 MW ",
                 "VN: 4.5 MW ",
                 ],
        x = [0, 0.25, 0.5, 0.5, 0.75, 0.75, 0.75, 0.75, 0.75],  # 手动设置节点x轴坐标
        y = [0.5, 0.5, 0.7, 0.3, 0.8, 0.6, 0.4, 0.2, 0.0]        # 手动设置节点y轴坐标
    ),
    link = dict(
        source = [0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2],
        target = [1, 2, 3, 4, 5, 6, 7, 4, 5, 6, 7, 8],
        value = [35, 12, 1, 13.8, 11.3, 7.3, 2.8, 3.4, 2.4, 1.1, 1.5, 3.6],
    )
)])

方法3:合并极小流量(可选)

如果业务逻辑允许,可将极小流量的节点合并到其他节点中,减少布局复杂度,从根源避免异常情况。

内容的提问来源于stack exchange,提问作者Ruby Hoofwijk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 02:20:42