简体   繁体   中英

Colab: Python and Sankey in Plotly

I've got some simple code to test Sankey charts. It seems to stop before rendering the last "merging" flows to the last node. Any help / suggestions?

import plotly.graph_objs as go1

# one merged line - CORP01 bought CORP02
# second, occurring after first M&A - CORP04 buys CORP03
# third, occuring after second M&A - CORP04 buys CORP01

# data
source = [0,1,2,3,4,4,5,6,7,7,8,8]
target = [4,4,5,6,7,7,8,8,9,9,9,9]

# data to dict, dict to sankey
link = dict(source = source, target = target, value = value)

label = ["CORP01",   #source "0"
         "CORP02",   #source "1"
         "CORP03",   #source "2"
         "CORP04",   #source "3"
         "CORP01m",   #target "4" / source "4"
         "CORP03a",   #target "5" / source "5"
         "CORP04a",   #target "6"
         "CORP01m2",   #target "7" / source "7"
         "CORP04m",   #target "8" / source "8"
         "CORP04m2"]   #target "9"

node = dict(label = label, pad=20, thickness=10)
data = go1.Sankey(link = link, node=node)

# plot
fig = go1.Figure(data)
fig.show(renderer="colab")

The chart works until the last two flows are expected to merge into the final single node. Any thoughts?

As per the info from streetster - I neglected to add a list to address the value for the Sankey figure.

By adding a list for value , the chart rendered perfectly.

# data
source = [0,1,2,3,4,4,5,6,7,7,8,8]
target = [4,4,5,6,7,7,8,8,9,9,9,9]
value  = [2,2,2,2,2,2,2,2,2,2,2,2]

# data to dict, dict to sankey
link = dict(source = source, target = target, value = value)

label = ["CORP01",   #source "0"
         "CORP02",   #source "1"
         "CORP03",   #source "2"
         "CORP04",   #source "3"
         "CORP01m",   #target "4" / source "4"
         "CORP03a",   #target "5" / source "5"
         "CORP04a",   #target "6"
         "CORP01m2",   #target "7" / source "7"
         "CORP04m",   #target "8" / source "8"
         "CORP04m2"]   #target "9"

node = dict(label = label, pad=20, thickness=10)
data = go1.Sankey(link = link, node=node)

# plot
fig = go1.Figure(data)
fig.show(renderer="colab")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM