0

I am trying to plot a sankey chart and running into the issue that I am not able to plot same node interactions that happen in sequence. For example, I have sankey_df:

user |date          |Source  | Target | Price
1    |01-01-2022    |A       | B      | 10
1    |02-01-2022    |A       | B      | 15

This is how I plot it but it does not render (because I have the same source/target connections.

product_list = list(set(sankey['target']))+list(set(sankey['source']))
di = {element:index for index,element in enumerate(product_list)}
#apply to df to convert source/target names to numerics
sankey=sankey.replace({"source": di,"target": di })

# data
import plotly.graph_objs as go1
import plotly.offline as pyo
# Set notebook mode to work in offline
pyo.init_notebook_mode()
%matplotlib inline

source = list(sankey['source']) 
target = list(sankey['target']) 
value  = list(sankey['price']) 

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

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

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

When I run the code, it does not throw an error but it does not show anything. When I change the source/target relation to something unique, it renders. I want to allow same-sequenced interactions. How do I enable this?

r-beginners
  • 31,170
  • 3
  • 14
  • 32
titutubs
  • 355
  • 1
  • 9
  • I ran your code with the above code on Jupyter Notebook and python 3.8.8 with changing the last line to `fig.show()` and it ran fine showing bar on left (A) and right(B) and two links (10 and 15) as expected. Might have to do with colab rather than plotly.graph_objects sankey. Have you tried it on any other IDE? – Redox Jul 03 '22 at 14:12
  • @Redox You are right! That small change worked! Thanks a ton :) – titutubs Jul 03 '22 at 20:18

1 Answers1

1

Thanks for Redox, there was a small issue in the plotting synthax. Instead of fig.show(renderer="colab"), I applied fig.show() without issues.

titutubs
  • 355
  • 1
  • 9