Skip to content

Sankey node x and y positions could not be equal to 0 or np.nan #3002

@banderlog

Description

@banderlog
  1. We need to define both x and y or nothing will work allow provision of X coordinates only for Sankey charts #1732
  2. We cannot maintain the original position of a node (in R one could just add NA to that vector position)
  3. x and/or y should not be equal exactly to 0

Consider example below

# example from <https://plotly.com/python/sankey-diagram/>
import plotly.graph_objects as go

fig = go.Figure(data=[go.Sankey(
    arrangement = "perpendicular",
    node = dict(
      pad = 15,
      thickness = 20,
      line = dict(color = "black", width = 0.5),
      label = ["A1", "A2", "B1", "B2", "C1", "C2"],
      color = "blue",
    ),
    link = dict(
      source = [0, 1, 0, 2, 3, 3], # indices correspond to labels, eg A1, A2, A1, B1, ...
      target = [2, 3, 3, 4, 4, 5],
      value = [8, 4, 2, 8, 4, 2]
  ))])

fig.update_layout(title_text="Basic Sankey Diagram", font_size=10)
fig.show()

image

Now we want to put A2, B2, C2 before A1,B1,C1 on Y axis:

fig.data[0].node.x = [0., 0., 0.5, 0.5, 1, 1]
fig.data[0].node.y = [1, 0.5, 1, 0.5, 1, 0.5]
fig.show()

image

We are able to see that A1 and A2 did not change their places. That because they have 0 in x coordinates.
It is easy to fix:

fig.data[0].node.x = [1e-09, 1e-09, 0.5, 0.5, 1, 1]

We are not reloading Ys, just fixing Xs is enough.
Probably plotly.js problem on visualization, but plotly.python problem for letting this.

image

If we will do something like fig.data[0].node.x = [np.nan, 1e-09, 0.5, 0.5, 1, 1], A1 will behave in the same way as if it would be a 0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions