0

So I'm trying to generate a snakey diagram from this dataframe :

    a   b   v
0   0   0   1
1   0   2   1
2   0   3   2
3   1   0   1
4   1   1   1
5   1   2   2
6   1   3   2
7   2   0   1
8   2   1   3
9   3   0   1
10  3   1   2
11  3   3   1
12  3   4   1
13  3   5   1
14  4   3   1
15  5   2   1

here's my code :

import holoviews as hv
import plotly.graph_objects as go
import plotly.express as pex

hv.extension('bokeh')

hv.Sankey(df_test2, kdims=["a","b"], vdims=["v"])

I get this message error : Sankey diagrams only support acyclic graphs. I don't really understand why it's not working !

2 Answers2

1

As mozway explained, the problem is that your data includes cyclic links. These are not supported by Sankey diagram function in holoviews. However, you can plot Sankey diagram with cyclic elements using plotly (e.g. following this tutorial).

andrein
  • 71
  • 3
  • I was able to identify self-loops (ie. a-> a) and direct recursive cycles (ie. a-> b, b -> a) but does anyone know how to identify and remove more complicated recursive cycles (ie. a -> b -> c -> d -> a)? – Kamil Dec 03 '21 at 21:42
  • When I use plotly to generate a sankey diagram, it produces no error and no image :( https://stackoverflow.com/a/50880668/5986651 So not sure what @Kamil is talking about. :( – agent18 Jul 10 '22 at 09:06
0

The error is quite self-explanatory. You cannot draw the plot as some keys are inducing a cycle. For instance the first row would give 0->0->0->0->(…), which is impossible. Similarly, rows 1 and 7 give 0->2->0->2->(…). You have to make sure that elements in b cannot make loops by coming back through a.

From your dataset, here is a working example (picked 3 rows without recursionà:

hv.Sankey(df.iloc[1:4], kdims=["a","b"], vdims=["v"])
mozway
  • 194,879
  • 13
  • 39
  • 75
  • I understand the problem of cycles being induces. However, what if the columns `a` and `b` of the OP represent different things. `a` could be the number of children in a family and `b` could be the number of pets in the family. How would one go about visualizing that relationship in a Sankey diagram with holoviews? – JorenV Feb 11 '22 at 12:03