1

I have the following pandas dataframe

import pandas as pd



 df_dict = {'index': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
  11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
  24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
  38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51],
 'columns': ['from', 'to', 'week', 'N'],
 'data': [[0.0, 1.0, 3.0, 1],
  [0.0, 1.0, 4.0, 1], [0.0, 1.0, 5.0, 1], [0.0, 1.0, 6.0, 1],
  [0.0, 1.0, 10.0, 1], [0.0, 1.0, 12.0, 1], [0.0, 1.0, 14.0, 1],
  [0.0, 2.0, 9.0, 1], [0.0, 2.0, 10.0, 1], [0.0, 3.0, 21.0, 1],
  [0.0, 4.0, 4.0, 1], [0.0, 4.0, 7.0, 1], [0.0, 4.0, 10.0, 2],
  [1.0, 0.0, 2.0, 1], [1.0, 0.0, 3.0, 2], [1.0, 0.0, 7.0, 1],
  [1.0, 2.0, 16.0, 1], [1.0, 3.0, 3.0, 1], [1.0, 3.0, 4.0, 1],
  [1.0, 3.0, 5.0, 1], [1.0, 3.0, 7.0, 1], [1.0, 3.0, 12.0, 1],
  [1.0, 3.0, 22.0, 1], [1.0, 3.0, 30.0, 1], [1.0, 3.0, 32.0, 1],
  [1.0, 4.0, 4.0, 1], [1.0, 4.0, 5.0, 2], [3.0, 0.0, 2.0, 1],
  [3.0, 1.0, 2.0, 1], [3.0, 1.0, 3.0, 1], [3.0, 1.0, 5.0, 1],
  [3.0, 1.0, 7.0, 1], [3.0, 1.0, 8.0, 1], [3.0, 1.0, 11.0, 1],
  [3.0, 1.0, 20.0, 1], [3.0, 1.0, 27.0, 1], [3.0, 1.0, 34.0, 1],
  [3.0, 4.0, 4.0, 1], [3.0, 4.0, 6.0, 1], [3.0, 4.0, 11.0, 1],
  [3.0, 4.0, 12.0, 1], [4.0, 0.0, 3.0, 1], [4.0, 0.0, 5.0, 1],
  [4.0, 1.0, 2.0, 3], [4.0, 1.0, 6.0, 1], [4.0, 1.0, 7.0, 3],
  [4.0, 1.0, 10.0, 1], [4.0, 3.0, 3.0, 1], [4.0, 3.0, 4.0, 1],
  [4.0, 3.0, 6.0, 1], [4.0, 3.0, 15.0, 2], [4.0, 3.0, 24.0, 2]]}

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

where the head looks like this:

    from    to      week    N
0   0.0     1.0     3.0     1
1   0.0     1.0     4.0     1
2   0.0     1.0     5.0     1
3   0.0     1.0     6.0     1
4   0.0     1.0     10.0    1

I would like to create a sankey plot where the diagram would be split into a grid of dimensions:

len(df['from'].unique()) times len(df['week'].unique())

so on the x-axis there would be the weeks, and on the y-axis would be the from, so at every week the from and the to would be the nodes of the diagram.

In the head example we would have 5 lines starting from "node 0" (at the very left side of the diagram) and:

  • one line going to "node 1" at week 3
  • one line going to "node 1" at week 4 (this node is a bit more to the right of the "node 1" of week 3 etc)
  • one line going to "node 1" at week 5 etc

So something that looks like this:

enter image description here

Is there a way to do that with plotly ?

quant
  • 4,062
  • 5
  • 29
  • 70

0 Answers0