[Spyder]Sankey Chart is not generated

Hi Team,

  1. I have below dataframe.
source,target,count
SCM,SOC,39
SOC,Open SOC Page,39
Open SOC Page,Expand Display Option,2
Open SOC Page,Expand Export,3
Open SOC Page,Hide Direct Report,2
Open SOC Page,Level Up,1
Open SOC Page,Open Position Card,2
Open SOC Page,Open SOC Page,7
Open SOC Page,People Search,15
Open SOC Page,Position Search,2
Open SOC Page,View Less,2
Open SOC Page,exit,3
Expand Display Option,SCM.SOC.SWITCH_REPORT_TYPE,1
Expand Display Option,Switch Show on Card,1
Expand Export,Hide Direct Report,1
......
......

Blockquote

  1. I have below code to generate sankey
import plotly.graph_objects as go
from plotly.offline import plot
import pandas as pd
import plotly as py

# read 'file:///C:/Users/I073341/.spyder-py3/step13_replaceAt.csv'
df = pd.read_csv('sankey.csv')

labelListTemp1 = list(set(df['source'].values))
labelListTemp2 = list(set(df['target'].values))
labelListTemp = labelListTemp1 + labelListTemp2
 # remove duplicates from labelList
sankey_node = list(dict.fromkeys(labelListTemp))
sankey_source = df['source']
sankey_target = df['target']
sankey_value = df['count']

fig = go.Figure(data=[go.Sankey(
    node = dict(
      pad = 15,
      thickness = 20,
      line = dict(color = "black", width = 0.5),
      label = sankey_node,
      color = "blue"
    ),
    link = dict(
      source = sankey_source, # indices correspond to labels, eg A1, A2, A2, B1, ...
      target = sankey_target,
      value = sankey_value
  ))])
    
fig.update_layout(title_text="Basic Sankey Diagram", font_size=10)
py.offline.plot(fig)

Blockquote

  1. The environment is Anaconda->Spyder, after I run the code, onthely the chart title is displayed, the sankey chart is not displayed. Can you help advise what’s wrong here?

Thanks,
Cherie

I figure out why.

They type of source and target must be number.
Resolution with adding two additional columns
df[‘sourceID’] = df[‘source’].apply(lambda x: labelList.index(x))
df[‘targetID’] = df[‘target’].apply(lambda x: labelList.index(x))

three reasons why you should buy plotly pro: support open source, get great support, host your plots and dashboards online