How to create a Sankey diagram in 30 seconds with Python

Sankey diagrams are a powerful tool for telling a compelling and engaging story about your data. These flow diagrams use arrows to visualize the flow of data between different stages, showing you at a glance how resources, energy, or costs move through a system. When used correctly, Sankey diagrams can provide deep insights that other charts and graphs might miss.

In this post, we’ll show you how to quickly create your own Sankey diagram, with no coding required! If you prefer to follow along with a video, be sure to check out the tutorial on our YouTube channel.

What is a Sankey Diagram?

A Sankey diagram is a specific type of flow diagram that represents the flow of data, resources, or material between entities. The width of each arrow (also called a “link”) is proportional to the flow rate, making it easy to spot the dominant contributors in your dataset.

These diagrams are commonly used to visualize energy usage, material flows, financial data, or any process where it's crucial to understand how resources are distributed. In business contexts, Sankey diagrams are invaluable for tracking expenses, customer journeys, and more.

How to Create a Sankey Diagram using AI

Step 1: Prepare Your Data

The first step in creating a Sankey diagram is to have your data in the correct format. The easiest way is to provide a CSV or Excel file with the following three columns:

  • From node: The source of the flow
  • To node: The destination of the flow
  • Amount: The quantity or value being transferred from the source to the destination

For example, if you're visualizing product revenue, the first few rows of your file might look like this:

From           | To                               | Amount

Product A | Product Revenue | 200

Product B | Product Revenue | 100

Make sure your data is organized in this simple structure to ensure a smooth visualization experience. If you have more than a few node, you can also upload your raw data and ask the AI to format it for you.

Step 2: Generate the Chart

Now that your data is ready, it's time to generate the Sankey diagram. We’ll be using Fabi.ai to do the heavy lifting, so no need to worry about writing code. Just follow these steps:

  1. Upload your data: Simply upload your CSV or Excel file to Fabi.ai.
  2. Ask the AI to create a Sankey diagram: Type in a simple command like, "Generate a Sankey diagram," and the AI will handle the rest.
  3. Add to Smartbook: Once generated, you can easily add the chart to your Smartbook for future analysis or reporting.

And that's it! Fabi.ai’s AI and python integration makes this process incredibly simple and fast, so you can focus on analyzing your data instead of writing complex code.

And of course, if you want to publish your Sankey diagram as an interactive chart for your peers, you can quickly publish it directly from the Fabi.ai Smartbook.

Bonus: For the DIYers

If you're comfortable with Python and want to generate a Sankey diagram on your own, here's a sample code snippet that the AI can generate for you using Plotly:

import plotly.graph_objects as go

# Define the data
source = [0, 1, 0, 2, 3, 3]
target = [2, 3, 3, 4, 4, 5]
value = [8, 4, 2, 8, 4, 2]

# Create the Sankey diagram
fig = go.Figure(go.Sankey(
    node = dict(
      pad = 15,
      thickness = 20,
      line = dict(color = "black", width = 0.5),
      label = ["Product A", "Product B", "Product Revenue", "Shipping", "Marketing", "Net Profit"],
      color = "blue"
    ),
    link = dict(
      source = source,
      target = target,
      value = value
  )))

# Show the diagram
fig.show()

This code generates a simple Sankey diagram, but with Fabi.ai, you can get a lot more done in a fraction of the time by letting the AI handle it for you.

Creating your first Sankey diagram made easy

Sankey diagrams are a great way to communicate the flow of resources or data within your organization. With Fabi.ai, you can create these insightful visualizations in just a few clicks—no coding required. Be sure to try it out for free to show off your data!

"I was able to get insights in 1/10th of the time it normally would have"

Don't take our word for it, give it a try!