Python Plotly 桑基图未显示
我正在使用 Jupyter Notebook 的 Python 3 (ipykernel),想通过导入一个包含数据的 json 文件来创建一个 Sankey 图。运行代码时没有报错,但输出却是空的,没有任何图表。
我参考了这里的一个例子:“更复杂的带有彩色链接的 Sankey 图”:https://plotly.com/python/sankey-diagram/#reference
- 我检查了 json 文件,确保没有循环引用(也就是说,两个节点没有在两个不同的条目中互为源和目标,比如:条目1: 源=A,目标=B;条目2: 源=B,目标=A)。
- 我还看到另一个帖子提到目标数量必须大于源数量 https://community.plotly.com/t/cannot-plot-sankey-diagram/9734,我也检查了这个,但这不是问题所在。
- 我在这里找到另一个帖子 https://community.plotly.com/t/sankey-diagram-appearing-as-blank/4959/7,提到要给标签添加前缀,但我不太明白这是什么意思。
有什么想法吗?谢谢!
这是我的代码:
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"]
}
]
}
]
}
}
1 个回答
0
这里有两个网站可以帮助你在Jupyter Notebook中输出plotly图表:
第一个是通过使用额外的库来实现的,链接在这里:Plotly文档。
第二个是通过更改或明确调用渲染器来实现的,链接在这里:Plotly渲染器。
希望这两个选项中有一个能对你有帮助。
编辑
我测试了代码和JSON,结果都很好,只要把最后一行改成fig.show(renderer="browser")
就可以了。过去我在检查Plotly图形是否成功创建时,通常会采取以下步骤。首先,检查fig.data
里是否有内容。这里应该包含一个元组,至少有一个项目,也就是一个Sankey图。如果是这样,并且确认图形已经创建,第二步就是逐个尝试找到适合你环境的渲染器。经过一段时间,你就会知道哪些渲染器适合你了。