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

Jupyter Notebook中Plotly Sankey图无输出问题求助

问题描述

在Python 3的Jupyter Notebook(ipykernel)环境中,尝试导入JSON数据文件并用Plotly创建Sankey图。运行代码后无报错,但仅显示空白输出。

已排查以下内容:

  • 检查JSON数据,确认无循环链接(不存在同一对节点互为源和目标的情况);
  • 验证目标节点编号均不小于源节点编号;
  • 了解到有帖子提到需为标签添加前缀,但未明确具体操作方式。

附上代码和JSON数据,寻求解决思路。

代码

import plotly.graph_objs as go
import json

with open("sankeywhst.json", 'r') as j:
     data = json.loads(j.read())

fig = go.Figure(data=[go.Sankey(
    valueformat = ".0f",
    valuesuffix = "TWh",
    # Define nodes
    node = dict(
      pad = 15,
      thickness = 15,
      line = dict(color = "black", width = 0.5),
      label =  data['data'][0]['node']['label'],
      color =  data['data'][0]['node']['color']
    ),
    # Add links
    link = dict(
      source =  data['data'][0]['link']['source'],
      target =  data['data'][0]['link']['target'],
      value =  data['data'][0]['link']['value'],
      label =  data['data'][0]['link']['label'],
      color =  data['data'][0]['link']['color']
))])

fig.update_layout(title_text="Flow of WHST Files",
                  font_size=10)
fig.show()

JSON数据

{"data": [
        {
            "type": "sankey",
            "domain": {
                "x": [
                    0,
                    1
                ],
                "y": [
                    0,
                    1
                ]
            },
            "orientation": "h",
            "valueformat": ".0f",
            "valuesuffix": "TWh",
            "node": {
                "pad": 15,
                "thickness": 15,
                "line": {
                    "color": "black",
                    "width": 0.5
                },
                "label": [
                    "All potentially generated",
                    "Generated",
                    "Not generated",
                    "Ingested within 48 hours",
                    "Ingested after 48 hours",
                    "Processed within 48 hours",
                    "Processed after 48 hours (already late)",
                    "Visualized within 48 hours",
                    "Visualized after 48 hours (otherwise on time)",
                    "Visualized after 48 hours (already late)",
                    "Excluded Fitbit Sleep files",
                    "Not intended for visualization: Fitbit Steps files",
                    "Not intended for visualization: Fitbit Steps files (already late)",
                    "Not intended for visualization: Fitbit Steps files (never generated)",
                    "Never visualized (otherwise on time)",
                    "Never visualized (already late)",
                    "Never visualized (never generated)"
                ],
                "color": [
                    "rgba(120, 0, 189, 0.8)",
                    "rgba(61, 0, 255, 0.8)",
                    "rgba(217, 0, 0, 0.8)",
                    "rgba(45, 100, 216, 0.8)",
                    "rgba(255, 128, 54, 0.8)",
                    "rgba(0, 210, 143, 0.8)",
                    "rgba(210, 94, 0, 0.8)",
                    "rgba(18, 197, 0, 0.8)",
                    "rgba(255, 210, 0, 0.8)",
                    "rgba(210, 94, 0, 0.8)",
                    "rgba(195, 0, 197, 0.8)",
                    "rgba(158, 0, 197, 0.8)",
                    "rgba(210, 94, 0, 0.8)",
                    "rgba(217, 0, 0, 0.8)",
                    "rgba(255, 210, 0, 0.8)",
                    "rgba(210, 94, 0, 0.8)",
                    "rgba(217, 0, 0, 0.8)"
                    ]
            },
            "link": {
                "source": [
                    0,
                    0,
                    1,
                    1,
                    2,
                    2,
                    3,
                    4,
                    5,
                    5,
                    5,
                    5,
                    5,
                    6,
                    6,
                    6    
                ],
                "target": [
                    1,
                    2,
                    3,
                    4,
                    13,
                    16,
                    5,
                    6,
                    10,
                    7,
                    8,
                    11,
                    14,
                    9,
                    12,
                    15  
                ],
                "value": [
                    924,
                    156,
                    891,
                    33,
                    24,
                    132,
                    891,
                    33,
                    9,
                    279,
                    410,
                    193,
                    0,
                    24,
                    8,
                    1
                ],
                "color": [
                    "rgba(120, 0, 189, 0.2)",
                    "rgba(120, 0, 189, 0.2)",
                    "rgba(61, 0, 255, 0.2)",
                    "rgba(61, 0, 255, 0.2)",
                    "rgba(217, 0, 0, 0.2)",
                    "rgba(217, 0, 0, 0.2)",
                    "rgba(45, 100, 216, 0.2)",
                    "rgba(255, 128, 54, 0.2)",
                    "rgba(0, 210, 143, 0.2)",
                    "rgba(0, 210, 143, 0.2)",
                    "rgba(0, 210, 143, 0.2)",
                    "rgba(0, 210, 143, 0.2)",
                    "rgba(0, 210, 143, 0.2)",
                    "rgba(210, 94, 0, 0.2)",
                    "rgba(210, 94, 0, 0.2)",
                    "rgba(210, 94, 0, 0.2)"
                ],
                "label": [
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    ""
                ]
            }
        }],
    "layout": {
        "title": {"text": "WHST Combined Data Flow"},
        "width": 1118,
        "height": 772,
        "font": {
            "size": 10
        },
        "updatemenus": [
            {
                "y": 1,
                "buttons": [
                    {
                        "label": "Light",
                        "method": "relayout",
                        "args": [ "paper_bgcolor", "white" ]
                    },
                    {
                        "label": "Dark",
                        "method": "relayout",
                        "args": [ "paper_bgcolor", "black"]
                    }
                ]
            },
            {
                "y": 0.9,
                "buttons": [
                    {
                        "label": "Thick",
                        "method": "restyle",
                        "args": [ "node.thickness", 15 ]
                    },
                    {
                        "label": "Thin",
                        "method": "restyle",
                        "args": [ "node.thickness", 8]
                    }
                ]
            },
            {
                "y": 0.8,
                "buttons": [
                    {
                        "label": "Small gap",
                        "method": "restyle",
                        "args": [ "node.pad", 15 ]
                    },
                    {
                        "label": "Large gap",
                        "method": "restyle",
                        "args": [ "node.pad", 20]
                    }
                ]
            },
            {
                "y": 0.7,
                "buttons": [
                    {
                        "label": "Snap",
                        "method": "restyle",
                        "args": [ "arrangement", "snap" ]
                    },
                    {
                        "label": "Perpendicular",
                        "method": "restyle",
                        "args": [ "arrangement", "perpendicular"]
                    },
                    {
                        "label": "Freeform",
                        "method": "restyle",
                        "args": [ "arrangement", "freeform"]
                    },
                    {
                        "label": "Fixed",
                        "method": "restyle",
                        "args": [ "arrangement", "fixed"]
                    }
                ]
            },
            {
                "y": 0.6,
                "buttons": [
                    {
                        "label": "Horizontal",
                        "method": "restyle",
                        "args": [ "orientation", "h" ]
                    },
                    {
                        "label": "Vertical",
                        "method": "restyle",
                        "args": [ "orientation", "v"]
                    }
                ]
            }
        ]
    }
}
解决思路

核心问题定位

你的Sankey图空白的主要原因是链接列表中存在值为0的条目(对应source=5、target=14的那条链接)。Plotly在渲染Sankey图时,若链接的value为0,会导致整个图表无法正常渲染,直接显示空白。

另外,你的JSON文件其实是完整的Plotly Figure格式(包含data和layout),无需手动提取节点和链接重新构建Sankey对象,可以直接加载使用,简化代码的同时避免手动拼接可能出现的错误。

修复方案

方案1:移除值为0的链接条目

修改JSON中的link.value数组,删除值为0的元素,同时对应删除source、target、color、label数组中相同位置的元素。修改后的链接部分如下:

"link": {
    "source": [0,0,1,1,2,2,3,4,5,5,5,5,6,6,6],
    "target": [1,2,3,4,13,16,5,6,10,7,8,11,9,12,15],
    "value": [924,156,891,33,24,132,891,33,9,279,410,193,24,8,1],
    "color": [
        "rgba(120, 0, 189, 0.2)",
        "rgba(120, 0, 189, 0.2)",
        "rgba(61, 0, 255, 0.2)",
        "rgba(61, 0, 255, 0.2)",
        "rgba(217, 0, 0, 0.2)",
        "rgba(217, 0, 0, 0.2)",
        "rgba(45, 100, 216, 0.2)",
        "rgba(255, 128, 54, 0.2)",
        "rgba(0, 210, 143, 0.2)",
        "rgba(0, 210, 143, 0.2)",
        "rgba(0, 210, 143, 0.2)",
        "rgba(0, 210, 143, 0.2)",
        "rgba(210, 94, 0, 0.2)",
        "rgba(210, 94, 0, 0.2)",
        "rgba(210, 94, 0, 0.2)"
    ],
    "label": ["","","","","","","","","","","","","","",""]
}

方案2:直接加载完整的Plotly Figure

既然你的JSON已经是标准的Plotly Figure结构,无需手动构建Sankey对象,直接用go.Figure()加载整个JSON数据即可,代码简化为:

import plotly.graph_objs as go
import json

with open("sankeywhst.json", 'r') as j:
    fig_data = json.load(j)

fig = go.Figure(**fig_data)
fig.show()

注意:使用此方案前,同样需要先移除JSON中值为0的链接条目,否则仍会显示空白。

额外验证点

  • 确保所有节点的标签、颜色数组长度一致,链接的source、target、value、color数组长度一致;
  • 节点编号需从0开始连续或存在对应的标签(你的数据满足这一点);
  • Jupyter Notebook中若仍显示异常,可尝试更新Plotly版本:pip install --upgrade plotly

内容的提问来源于stack exchange,提问作者Mimi Curro

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 12:37:02