Useful Data Tips

Sankey Diagrams for Flow Visualization

⏱️ 26 sec read πŸ“Š Data Visualization

Sankey diagrams visualize flows between nodes with width proportional to quantity. Perfect for showing user journeys, energy flows, budget allocation, and any process where items move between states.

Creating a Sankey Diagram

import plotly.graph_objects as go

# Website user flow: Landing β†’ Pages β†’ Actions
fig = go.Figure(data=[go.Sankey(
    node=dict(
        pad=15,
        thickness=20,
        label=["Homepage", "Product Page", "About", "Cart", "Checkout", "Purchase"],
        color=["blue", "orange", "green", "yellow", "red", "purple"]
    ),
    link=dict(
        source=[0, 0, 0, 1, 1, 3, 4],  # From node indices
        target=[1, 2, 3, 3, 4, 4, 5],  # To node indices
        value=[1000, 300, 500, 400, 300, 600, 200]  # Flow quantities
    )
)])

fig.update_layout(title="Website User Journey", font=dict(size=12))
fig.show()

Common Use Cases

Budget Allocation

# Company budget flow from revenue to expenses
nodes = ["Revenue", "Operating Costs", "Marketing", "R&D",
         "Salaries", "Infrastructure", "Advertising", "Social Media"]

links = {
    'source': [0, 0, 0, 1, 2, 2],  # Revenue splits to three departments
    'target': [1, 2, 3, 4, 6, 7],
    'value': [500000, 200000, 300000, 400000, 100000, 100000]
}

# Shows where money flows through organization

Customer Journey

# Marketing channel β†’ Landing page β†’ Conversion
source: ["Facebook", "Google", "Email", "Facebook", "Google", "Email"]
target: ["Landing A", "Landing A", "Landing B", "Convert", "Convert", "Convert"]
value: [1000, 1500, 800, 150, 300, 120]

# Visualizes conversion funnel from multiple sources

Energy Flow

# Energy production β†’ consumption
Production sources β†’ Distribution β†’ End uses
Coal/Gas/Solar β†’ Grid β†’ Residential/Industrial/Commercial

Width shows proportion of total energy

Sankey Best Practices

Multi-Level Sankey

import plotly.graph_objects as go

# Traffic source β†’ Landing page β†’ Action β†’ Outcome
fig = go.Figure(data=[go.Sankey(
    arrangement='snap',
    node={
        'label': [
            # Sources
            'Organic', 'Paid', 'Social',
            # Landing pages
            'Home', 'Product', 'Blog',
            # Actions
            'Browse', 'Add to Cart', 'Purchase'
        ],
        'color': ['#1f77b4'] * 3 + ['#ff7f0e'] * 3 + ['#2ca02c'] * 3
    },
    link={
        'source': [0,0,1,1,2,2, 3,3,4,4,5,5, 6,7,7],
        'target': [3,4,4,5,3,5, 6,7,6,7,6,8, 7,8,8],
        'value':  [500,300,400,200,300,100, 400,200,300,300,200,100, 100,150,250]
    }
)])

fig.update_layout(title="Multi-Stage User Journey")
fig.show()

When to Use Sankey Diagrams

Pro Tip: Sankey diagrams excel at showing proportional flows and splits. Use them when you need to show how quantities divide and merge through a process. Keep it simpleβ€”too many nodes create confusion!

← Back to Visualization Tips