Sergio Polimante
Sergio Polimante

Reputation: 179

Plotly Sankey Diagram, problem with broken edge

I am using Sankey plot, from plotly, and for some reason I keep getting a weird shape on the edges of my graph, as seen on the image:

enter image description here

I expected that the green line should flow directly to from one edge to another, instead of doing this weird loop.

To reproduce the error, you can run the code below:

import plotly.graph_objects as go

fig = go.Figure(go.Sankey(
    arrangement = "snap",
    node = {
        "label": ["A", "B", "C", "D", "E", "F"],
        "x": [0, 0.1, 0.5, 0.2, 0.3, 0.5],
        "y": [0, 0.5, 0.2, 0.4, 0.2, 0.3],
        'pad':10},  # 10 Pixels
    link = {
        "source": [0, 0, 1, 2, 5, 4, 3, 5],
        "target": [5, 3, 4, 3, 0, 2, 2, 3],
        "value": [1, 2, 1, 1, 1, 1, 1, 2]}))
fig.show()

you should get this result:

enter image description here

The problem is the edge:

enter image description here

Does anybody know how I can fix it?

Thanks.

Upvotes: 1

Views: 754

Answers (2)

Brandon Kuczenski
Brandon Kuczenski

Reputation: 985

Not a solution, unfortunately, but a more complete problem statement. The problem is not limited to graphs when node positions are specified manually. It depends on the graph topology.

I developed the following minimal example containing five links among 4 nodes. if any of the link values below are set to 0, the problem goes away:

import plotly.graph_objects as go
nodes = {'label': ['node 0', 'node 1', 'node 2', 'node 3']}
links = {'source': [0, 1, 2, 2, 3], 'target': [3, 0, 0, 3, 2], 'value': [1, 1, 1, 1, 1]}
go.Figure(go.Sankey(node=nodes, link=links)).show()

In the correct rendering, there would be a straight-line trace from node 2 to node 0

I opened another bug: https://github.com/plotly/plotly.py/issues/3587

Upvotes: 0

Adrien Jacob
Adrien Jacob

Reputation: 13

I had the same issue with a Sankey, involving a loop, under Plotly 5.5.0... I tried your test case, and simplified it a bit more to isolate the problem:

import plotly.graph_objects as go
fig = go.Figure(go.Sankey(
    node = {"label": ["First node", "Last node"],
        "x": [0.6, 0.2],
        "y": [0.2, 0.6]},
    link = {
        "source": [0, 1],
        "target": [1, 0],
        "value":  [1, 1]
}))
fig.show()

It looks like a bug from Plotly (I submitted an issue), but thankfully there's a way around: it doesn't happen if you make sure the 'x' position of the first node is lower than the 'x' position of the last one. You may try again the code above, just swapping 'x' positions:

    "x": [0.2, 0.6],

In short, you just need to sort all your nodes by ascending 'x' position (which means of course modifying link values accordingly, as they are indice-dependant).

Upvotes: 1

Related Questions