How can fixed scales be set in sankey python plots

I’m trying to produce Sankey plots that have the same scale as each other.
Any help would be greatly appreciated.

Hi @span, I am still confused by what you mean by β€˜Sankey plots that have the same scale as each other’ Do you mean the output size of the Sankey? e.g. 1024px X 768px? which you can specify in Plotlt graph objects

# Update chart with some customisations
fig.update_layout(
    hovermode='x',
    paper_bgcolor='#51504f',
    width=1000,
    height=1000,
    margin={'t':50,'b':20}
)

Can you share what you have or mock up some data that shows your issue and then we can get you to where you need to be?

Regards

Tom

Hi Tom,

Thank you so much for getting back to me.

I want to be able to compare two sankeys, so that the number of pixels is fixed to the unit being represented.

X pixels / unit across different sankeys. Is that possible?

And

The orange and blue parts of the second should add up to the orange in the first in terms of space taken up (y-axis)

Does that make sense, or do I need to put some code together?

Best regards and thank you for your help,

Anna

image004.jpg

Hi Anna,

Sorry for the late reply. I looked into the documentation around this and I do not believe there is a way to specify the pixels/unit that you talk about. The only real control you have over the size is with the size of the chart itself specifying its height and width.

I might be wrong. perhaps @adamschroeder or @AnnMarieW may be able to shed some insights on this?

Hey @span, welcome to the forums.

If I understood your question correctly, the only way to achieve this, is setting the pad parameter to 0.

import plotly.graph_objects as go

s1 = go.Sankey(
    node = dict(
        pad = 0,
        # ^^ this defines the distance between the nodes. 
        thickness = 15,
        line = dict(color = "black", width = 0.5),
        label = ["A1", "A2", "B1"],
        color = "blue"
    ),
    link = dict(
        source = [0, 1],
        target = [2, 2],
        value = [8, 4],
        color = ['red', 'gray']
    ),
    domain={'x': [0.0, 0.45]}
    # show on the left half of the figure
)

s2 = go.Sankey(
    node = dict(
        thickness = 15,
        line = dict(color = "black", width = 0.5),
        label = ["A1", "B1"],
        color = "blue"
    ),
    link = dict(
        source = [0],
        target = [1],
        value = [12],
        color = ['red']
    ),
    domain={'x': [0.55, 1.0]}
    # show on the right half of the figure
    
)

# create figure
fig = go.Figure(data=[s1, s2])

# create some visual reference
fig.add_hline(y=-1)
fig.add_hline(y=1)

# hide axes
fig.update_xaxes(visible=False)
fig.update_yaxes(visible=False)

fig.show()

Here an image with pad=15

2 Likes

Hi,

Thanks for the information, I will try it out.

And let you know if it works

Cheers,

Anna

image001.png

image002.png

image003.png

1 Like

I tried it, but unfortunately for my data i bring in an extra source and then that shrinks the sources & targets to fit the page or figure


The purple node on the far right has the same value in both diagrams but gets squeezed to make space for the green (which is coming from the blue).
Thanks anyway for thinking about my problem.