为什么plotly的桑基图没有绘制我的所有源节点?

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

所以我试图绘制一个桑基图,它应该显示 13 个源节点到 4 个不同的目标节点,但我不知道为什么它不显示 13 个而只显示 6 个?这是我的代码和输出。它成功绘制了所有 4 个目标节点,但只有 6 个源节点,并且顺序也非常奇怪。它绘制了“test”(第一个节点)、“test3”(第四个节点)、“test7”、“test10”、“test12”、“test8”。

import plotly.graph_objects as go
source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
target = [13, 13, 13, 14, 14, 14, 14, 15, 16, 16, 16, 16, 16]
value = [1.5731754916099199, 0.0, -0.6622546030040232, 0.1803752820589049, -0.12705291484645911, -0.11239581604114621, -0.33000230488318316, 0.10184310050452802, 0.23013722041176082, -0.052664676881438124, 85.1250672340013, -0.006987341636945739, 11.9843590360198]
labels = ["test", 'test1', 'test2', 'test3', 'test4', 'test5', 'test6', 'test7', 'test8', 'test9', 'test10', 'test11', 'test12', 'TARGET1', 'TARGET2', 'TARGET3', 'TARGET4']
link = dict(source=source, target=target, value=value)
node = dict(label=labels, pad=35, thickness=10)
data = go.Sankey( textfont=dict(color="rgba(0,0,0,0)", size=1), link=link, node=node)
fig = go.Figure(data)
fig.update_layout(
        hovermode = 'x',
        title="Sankey",
        font=dict(size=8, color='white'),
        paper_bgcolor='#51504f'
    )
fig.show()

enter image description here

python python-3.x plotly plotly-python sankey-diagram
2个回答
2
投票

问题是我的桑基值列表中有负值。显然你不能这样做。如果您尝试使用负百分比的饼图,这是一个类似的问题(它只是不起作用或看起来不错)。为了解决我的问题,我在标签上添加了一个负号,然后对所有负数进行了绝对值计算。


0
投票

使

plotly.graph_objs.sankey.link.value
等于 0 与隐藏该特定链接是一样的。

如果这是节点正在接收或发送的唯一链接,那么该节点...简单地...消失(在

plotly.graph_objs.Figure(layout=)
处理期间被忽略。

© www.soinside.com 2019 - 2024. All rights reserved.