5

I have the following pandas dataframe:

df_dict = {'index': [15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
  26, 27, 28, 29, 30, 31, 32, 33, 34],
 'columns': ['cluster', 'week', 'color_line'],
 'data': [[3, 1, 'green'],
  [3, 2, 'green'],
  [3, 3, 'green'],
  [3, 4, 'green'],
  [3, 5, 'green'],
  [3, 6, 'green'],
  [4, 7, 'green'],
  [3, 8, 'yellow'],
  [3, 9, 'yellow'],
  [4, 10, 'yellow'],
  [3, 11, 'green'],
  [3, 12, 'yellow'],
  [3, 13, 'yellow'],
  [4, 14, 'yellow'],
  [4, 15, 'red'],
  [4, 16, ' orange'],
  [3, 17, 'yellow'],
  [3, 18, 'green'],
  [4, 19, 'red'],
  [3, 20, 'green']]}

to_plot_2 = pd.DataFrame(index=df_dict['index'], columns=df_dict['columns'], data=df_dict['data'])

I would like to create a sankey diagram, where it would show the transition by week of the cluster to the cluster_target (which is just a shift of the cluster column), colored by the color_line

I have tried this :

import plotly.graph_objects as go
fig = go.Figure(data=[go.Sankey(
    node = dict(
      pad = 15,
      thickness = 20,
      line = dict(color = "black", width = 0.5),
      label = [0,1,2,3,4],
      color = "blue"
    ),
    link = dict(
      source = to_plot_2['cluster'],
      target = to_plot_2['cluster_target'],
      value = [1] * len(to_plot_2),
      color = to_plot_2['color_line']
  ))])


fig.show()

I do not know how to add the week element on the x-axis here, any ideas?

UPDATE

I have tried this

fig = go.Figure(data=[go.Sankey(
    arrangement='snap',
    node = dict(
      pad = 20,
#       thickness = 20,
#       line = dict(color = "black", width = 0.5),
      label = list(to_plot_2['cluster'].astype(str)),
      color = "blue",
      x = [i * 1/(to_plot_2.week.max()) for i in list(range(0,to_plot_2.week.max(),1))],
      y = list(to_plot_2['cluster'] * (1/5))
    ),
    link = dict(
      source = list(range(0,to_plot_2.week.max())),
      target = list(range(1,to_plot_2.week.max()+1)),
      value = [1] * len(to_plot_2),
      color = to_plot_2['color_line']
  ))])


fig.show()

but the output looks like this:

enter image description here

so the labels are not on the nodes but on the links. Any ideas ?

quant
  • 4,062
  • 5
  • 29
  • 70
  • 1
    Please share your data like [this](https://stackoverflow.com/questions/63163251/pandas-how-to-easily-share-a-sample-dataframe-using-df-to-dict/63163254#63163254) – vestland Apr 08 '21 at 11:37
  • 1
    @vestland edited the question. tnx – quant Apr 08 '21 at 11:58

0 Answers0