0

Empty plot

I'm new to python/spyder, and I'm trying to make a sankey diagram, but no data is included in the plot, it's just empty. I found out that I need to convert from dataframe to lists, but this hasn't helped, and the plot is still empty. I kept it very simple, and pretty much copied it straight from a guide from plotly, and just imported my own data. Here is my code, where I just removed the filepath. Can anyone tell my what my mistake is? edit to add image of xlsx file : xlsx file
edit 2 image of new Plot

import plotly.graph_objects as go
import pandas as pd
# go.renderers.default = "browser"
df = pd.read_excel (r'C:\filepath\data.xlsx') 

labels=pd.DataFrame(df, columns= ['Label'])
label_list=labels.values.tolist()
sources=pd.DataFrame(df, columns= ['Source'])
source_list=sources.values.tolist()
targets=pd.DataFrame(df, columns= ['Target'])
target_list=targets.values.tolist()
values=pd.DataFrame(df, columns= ['Value'])
value_list=values.values.tolist()


fig = go.Figure(data=[go.Sankey(
    node = dict(
      pad = 15,
      thickness = 20,
      line = dict(color = "black", width = 0.5),
      label = label_list,
      color = "blue"
    ),
    link = dict(
      source = source_list, 
      target = target_list,
      value = value_list
  ))])

fig.update_layout(title_text="Basic Sankey Diagram", font_size=10)
fig.show()

1 Answers1

0

Please check the snippet with random data. sankey

When you are reading from excel, it gives you dataframe only. You dont need to store it again to dataframe.

Neither you need to convert it to list. You can pass dataframe column directly to label,source,value and target

import plotly.graph_objects as go
import pandas as pd
df = pd.read_excel (r'data.xlsx')
print(df)
"""
  Label  Source  Target  Value
0    A1       0       2      8
1    A2       1       3      4
2    B1       0       3      2
3    B2       2       4      8
4    C1       3       4      4
5    C2       3       5      2
"""
fig = go.Figure(data=[go.Sankey(
    node = dict(
      pad = 15,
      thickness = 20,
      line = dict(color = "black", width = 0.5),
      label = df['Label'],
      color = "blue"
    ),
    link = dict(
      source = df['Source'],
      target = df['Target'],
      value = df['Value']
  ))])

fig.update_layout(title_text="Basic Sankey Diagram", font_size=10)
fig.show()

Tips

Your labels_list, source_list, target_list, value_list is not a list. It is a nested list.

If you want to store your dataframe columns to a list then you can do like this,

labels_list=df['Label'].tolist()
source_list=df['Source'].tolist()
target_list=df['Target'].tolist()
value_list=df['Value'].tolist()

For more details you can refer Sankey Diagram Plotly

Karthik
  • 2,181
  • 4
  • 10
  • 28
  • The values seem to be there now, but the labels are not. Can this be because not all labels are used yet? Or could there be another problem? – Tilde Lyngsø Sep 11 '20 at 14:51
  • labels should also be there. Did you check the spelling correctly? from excel and in the code @TildeLyngsø – Karthik Sep 11 '20 at 14:53
  • Label is spelled the same in both excel and the code. I just copied your code from your response, and it still doesn't show any labels. The issue might be something else then.. – Tilde Lyngsø Sep 11 '20 at 15:14
  • @Tilde Lyngso. Your `df['Label']` aint empty right? – Karthik Sep 11 '20 at 15:15
  • No, it prints everything, so its there, it just doesn't show up on the plot.. – Tilde Lyngsø Sep 11 '20 at 15:23
  • Can you post screenshot of your plot? @TildeLyngsø – Karthik Sep 11 '20 at 15:34
  • @TildeLyngsø I feel Your excel sheet `value` column is wrong. Why are you using `comma`? I dont get that values. Should it be decimal? – Karthik Sep 11 '20 at 15:55
  • Right, I forgot to change that, in my country comma is used for decimal. – Tilde Lyngsø Sep 11 '20 at 16:02
  • @TildeLyngsø Great . If you find my answer helpful you can accept it as answer – Karthik Sep 11 '20 at 16:03
  • Is your data actually relevant to sankey plot? @TildeLyngsø Or is it some random data you generated? – Karthik Sep 11 '20 at 16:07
  • It is relevant, it's for an overview of produced/imported and used/exported energy.. – Tilde Lyngsø Sep 11 '20 at 16:11
  • I will suggest you to read about sankey diagram via wikipedia and the link I attached in ansert. As each data should be interlinked, to display a flow among several fields and you are having negative data in `value` column. Please try to understand abt the structure of Sankey. I feel your data is not appropriate for sankey – Karthik Sep 11 '20 at 16:16
  • Thank you, I'll look into it – Tilde Lyngsø Sep 11 '20 at 16:19