Skip to content

node positions do not work with link value zero in Sankey #2152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
rubenbfeld opened this issue Feb 3, 2020 · 2 comments
Open

node positions do not work with link value zero in Sankey #2152

rubenbfeld opened this issue Feb 3, 2020 · 2 comments
Labels
bug something broken P3 backlog

Comments

@rubenbfeld
Copy link

rubenbfeld commented Feb 3, 2020

Hello,
I'm new to the plot.ly lib but I already discovered an issue.
If there is a node with:

  • no links
  • only links with the value of zero

the manual positioning doesn't work anymore except if you delete the relating coordinates of this node and keep the node in the list (see third example).

Below you can find a short testing code with the generated output.

working example

import plotly.graph_objects as go
data=go.Sankey(
    arrangement = "freeform",
    node = {
        "label": ["A", "B", "C", "D", "E", "F"],
        "x": [0.2, 0.1, 0.9, 0.7, 0.3, 0.9],
        "y": [0.1, 0.9, 0.2, 0.1, 0.9, 0.9],
        'pad':100},  # 10 Pixels 
    link = {
        "source": [0, 0, 1, 4, 4, 3, 3],
        "target": [1, 3, 4, 3, 5, 2, 5],
        "value": [3, 3, 5, 4, 10, 1, 7 ]})
fig_sankey = go.Figure(data=data)
fig_sankey.show()

newplot(6)

node with only one link with value of zero

import plotly.graph_objects as go
data=go.Sankey(
    arrangement = "freeform",
    node = {
        "label": ["A", "B", "C", "D", "E", "F"],
        "x": [0.2, 0.1, 0.9, 0.7, 0.3, 0.9],
        "y": [0.1, 0.9, 0.2, 0.1, 0.9, 0.9],
        'pad':100},  # 10 Pixels 
    link = {
        "source": [0, 0, 1, 4, 4, 3, 3],
        "target": [1, 3, 4, 3, 5, 2, 5],
        "value": [3, 3, 5, 4, 10, 0, 7 ]})
fig_sankey = go.Figure(data=data)
fig_sankey.show()

newplot(7)

kind of working again cause coordinates of C are deleted

import plotly.graph_objects as go
data=go.Sankey(
    arrangement = "freeform",
    node = {
        "label": ["A", "B", "C", "D", "E", "F"],
        "x": [0.2, 0.1, 0.7, 0.3, 0.9],
        "y": [0.1, 0.9, 0.1, 0.9, 0.9],
        'pad':100},  # 10 Pixels 
    link = {
        "source": [0, 0, 1, 4, 4, 3, 3],
        "target": [1, 3, 4, 3, 5, 2, 5],
        "value": [3, 3, 5, 4, 10, 0, 7 ]})
fig_sankey = go.Figure(data=data)
fig_sankey.show()

newplot(12)

@rubenbfeld rubenbfeld changed the title node positions do not work with link value 0 in Sankey node positions do not work with link value zero in Sankey Feb 4, 2020
@smarti57
Copy link

As of today, this is still an issue. While I find plotly generally useful and appreciate the work, the sankey interface is not a high point.

@Panbati
Copy link

Panbati commented Feb 9, 2022

Hi guys,

Don't know if this helps you, but I maybe found a workaround for this, at least for my use case.
This is my first post on Github, so excuse any weird rambling or lack of structure.

My understanding is that when a value = 0, it shifts the relationship between source and target.
My value dictionary consists of dataframe rows since I retrieve it from a SQL database.

I built a simple function which just checks if the data in values is 0 or not.

def test(index: int, values: list): if values[index] == 0: return None else: return index

I do this because I build my value, source and target as lists first, then throw them as a dictionary in link.
When you throw None in source or target, it just eliminates that specific flow. So in my mind, you can never have a node with a flow of 0, because that's technically just an empty flow.

So, originally my source :
source = [ 0,0 ,2,2 ,3,3 ,4,4 ,6,6,6 ]

So what I did was just replace all those numbers, since they are technically indexes for my dataframe. I implemented this into my source list. As a quick example my source then became:
source = [test(0, value), test(0, value), test(2, value), test(2, value), test(3, value), test(3, value), test(4, value), test(4, value), test(6, value), test(6, value), test(6, value)]

This should not change the structure, in the end leaving your formatting as is.

Let me know if you have any questions or of this works for you or not.
For me it works by removing the sources with dead targets.

@gvwilson gvwilson self-assigned this Jul 3, 2024
@gvwilson gvwilson removed their assignment Aug 2, 2024
@gvwilson gvwilson added P3 backlog bug something broken labels Aug 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something broken P3 backlog
Projects
None yet
Development

No branches or pull requests

4 participants