How to plot Basic Sankey diagram with Python's Plotly
Real case using Meta Q2 2022 Income statement
In this post, I will go through the basics of Plotly and Sankey diagram, and to generate a Sankey diagram to visualise the data from Meta.
What is Plotly?
There are a number of python libraries available for data visualisation, such as Matplotlib, Seaborn, to name a few. Plotly is one of such libraries. One advantage I appreciate Plotly is its interactivity. When making Sankey diagram, plotly seems more user friendly than Matplotlib.
What is Sankey diagram?
A sankey diagram is a visualization used to depict a flow from one set of values (sources) to another (targets). The things being connected are called nodes and the connections are called links. 1
How to plot a Sankey diagram?
Income statement is a good data source for Sankey diagram as it shows the flow of value from revenue to net profit.
Step1 : Import library
Step2 : Plot the diagram
A straight forward approach was taken to define the parameters for the diagram since it’s a simple dataset and easy to understand the logic of the code.
Step3 : Add title to the diagram
Not only title, but also logo and pics and texts could also be added to the diagram.
Step4 : beautify the diagram
Without any adjustments, the above diagram is what you get. Don’t worry, What you need to do is to click the diagram and move the colour blocks (link) to the place you prefer.
One downside is that it will get back to its original format once you run the code again.
If you know how to fix the new position of the diagram, would love to hear from you.
Open points:
When more data available to plot the diagram, a dataframe would be more appropriate rather than manual input.
I’m still trying to figure out how to change the colours of the texts, so different group would be assigned to different colours.
As mentioned earlier, I also would like to know if there’s better way to change the position of the nodes and the links.
Hope this short post could help you a bit.
Thanks for reading.
The complete code is available via Github.
References:
https://plotly.com/python/sankey-diagram/
https://developers.google.com/chart/interactive/docs/gallery/sankey
Seems like stackoverflow solved this exact issue
https://stackoverflow.com/questions/61152889/plotly-how-to-set-node-positions-in-a-sankey-diagram
Semui do you do more of this type of work?