DEV Community

Cover image for Visualize Financial Data With Sankey Diagrams in Python
Code_Jedi
Code_Jedi

Posted on • Edited on

6 3

Visualize Financial Data With Sankey Diagrams in Python

Today I'll be showing you a unique, versatile and useful way of visualizing data in python.


Sankey diagrams are a great, yet often overlooked method of visualizing important data such as: resource management and usage, financial data such as income and investments, time management and allocation, expenses and much more!


For example, let's take a SaaS company(let's call it DatavizWRLD.com) and Josh, the CEO wants to see the company's monthly income, profit, expenses, and money management. We can give him an easy way of visualizing all those with a Sankey diagram:
Sankey diagram
Sankey diagram hover
Sankey diagram hover 2


As you can see, this Sankey diagram gives Josh a simple and effective way of visualizing his company's monthly income, profit, expenses, and money management.


Building your own!

If you just want the code, voila( even though I advise that you stick around for the explanation because it might be difficult to understand from the beginning ):



import plotly.graph_objects as go

fig = go.Figure(data=[go.Sankey(
    node = dict(
    thickness = 5,
    label = ["Ads", "Net profit", "Total income", "ROI", "Paid services", "Investment", "Expenses", "Maintaining website", "Paying employes", "Advertising", "Personal expenses", "Savings"],
    color = "cyan"
    ),
    link = dict(

    # indices correspond to labels
    source = [0, 1, 4, 2, 3, 2, 6, 6, 6, 1, 1],
    target = [2, 5, 2, 1, 2, 6, 7, 8, 9, 10, 11],
    value = [60000, 40000, 90000, 100000, 10000, 60000, 10000, 30000, 20000, 20000, 40000]
))])

fig.update_layout(
    title="DatavizWRLD.com monthly income and income management of 2021( in USD )",
    font=dict(size = 12, color = 'black')
)

fig.show()


Enter fullscreen mode Exit fullscreen mode

Let's make sense of all this

  • First, we import "plotly.graph_objects" as "go".
  • Define "fig", this will be our Sankey diagram.
  • Characterize the parts of our Sankey diagram with: thickness, color, and labels.
  • Next, we define how and where the labels should connect to or disconnect from, as well as the value of each label. (The numbers on "source" and "target" are indices of the labels array).
  • Finally, we give our diagram a title colored black with a size of 12.

So there's that, a great and versatile yet underrated way of visualizing data in python.


Byeeeee👋

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay