Just published an article on Medium re Sankey diagrams

Hey All, In my continuing quest for self-improvement, I have just spent 24 hours getting into Sankey diagrams. They were black art to me.

I have documented my learnings in a medium article if anyone is interested,
https://medium.com/@twelsh37/understanding-plotly-sankey-charting-3ee263a81549

And yes there is probably typos, but I hope to spot them tomorrow. I think I got most of them

2 Likes

very informative article. Thanks for sharing, @twelsh37 :pray:

Thank you, Adam. It is through your Channel that I got into Plotly. It is my go-to visualization library. That long with some Dash stuff I am mocking up will make for a great dashboard.

2 Likes

Tom,
we actually have a pretty active community with members working on separate projects. If you’re interested, I would be happy to meet again and share about the ongoing community projects.

adam

Hi @twelsh37, first of all thank for sharing you knownledge about Sankey. Can I have a question that have you ever made Sankey diagram with same level 1 but level 2, level 3… is different? Something like image below.

So with your code, I can made separate Sankey as below:


So I’m curious is there anyway to merge them. Thank you.

Hey Hoatran,

Did you ever work out this Sankey/ It looks awesome!!

Sorry, it has taken me so long to get back to this. I hardly log in and look at stuff. It is an interesting Sankey. I will have to mock it up, but I don’t have an answer off the top of my head.

Regards

Tom

1 Like

Hi Tom,

Not really, I just want to know more about graphs so I tried to figure it out. I’m still stucking.

Ok Cool. I’ll take a look at it over the next few days.

I have just fixed a really stupid issue I was having with a Bar chart (DOH!!! facepalm x 2) so should be finished this soon.

Looks like it may be a pen and paper exercise to start with.

Regards

Tom

So I have played around a bit and can create the following manually. I will look at doing it programmatically later.

I marked up the flow you posed so you can see where each relates

And here is the Sankey. Please note 8 and 9 have been reversed.

Here is the code I used to do it manually

label = ['Google Search', 'YouTube', 'AdMob', 'Google Play', 'Google Cloud',
         'Other', 'Ad Revenue', 'Revenue', 'Gross Profit', 'Cost of Revenues',
         'Operating Profit', 'Operating Expenses', 'TAC', 'Others', 'Net Profit',
         'Tax', 'Other', 'R&D', 'S&M', 'G&A'
         ]

# Node Colors
color_for_nodes =['blue', 'blue', 'blue', 'gold', 'gold', 'gold', 'blue','blue','green', 'maroon',
                  'green', 'maroon', 'maroon','maroon', 'green', 'maroon','maroon', 'maroon', 'maroon',
                  'maroon', 'maroon']

color_for_links =['skyblue', 'skyblue', 'skyblue', 'yellow', 'yellow', 'yellow', 'skyblue', 'lightgreen',
                 'indianred', 'lightgreen', 'indianred', 'indianred', 'indianred', 'lightgreen', 
                  'indianred', 'indianred', 'indianred', 'indianred', 'indianred']
# Data 4
source4 = [0, 1, 2, 3, 4, 5, 6, 7, 7, 8, 8, 9, 9, 10, 10, 10, 11, 11, 11] 
target4 = [6, 6, 6, 7, 7, 7, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
value4 = [39.5, 7.1, 7.9, 6.9, 6.9, 0.88, 54.5, 37.9, 31.2, 17.1, 20.8, 11.8,
          19.3, 13.9, 2.3, 0.9, 10.3, 6.9, 3.6 ]

link4 = dict(source=source4, target=target4, value=value4, color=color_link)
node4 = dict(label = label, pad=35, thickness=20)
data4 = go.Sankey(link=link4, node=node)

fig1 = go.Figure(data4)
fig1.update_layout(
    hovermode='x',
    title='Stitching it all together',
    font=dict(size=10, color='white'),
    paper_bgcolor='#51504f'

)

fig1.update_traces(node_color = color_for_nodes,
                  link_color = color_for_links)
fig1.show()

Quite a bit of styling etc to be done but the basics are down

3 Likes

Thank you so much.

It worked for me.

I had a massive dive into this today and have learnt a tonne about Plotly and Sankeys.

Needless to say, I will publish a medium article, but here is my finished work on your issue.