Understand flow branches with a Sankey diagram

Visualize

Understand flow branches with a Sankey diagram

Created: Last updated: Read time: 1 min

When customers branch into different actions, a Sankey diagram makes the flow easy to follow. For interactive exploration, Plotly’s go.Sankey works well. You can inspect flow volume on hover and share as HTML.

import plotly.graph_objects as go

labels = [
    "Entry",
    "Free signup",
    "Cart abandon",
    "FAQ visit",
    "Bounce",
    "Paid conversion",
    "Return visit",
    "Churn",
]

sources = [0, 0, 0, 0, 1, 1, 1]
targets = [1, 2, 3, 4, 5, 6, 7]
values = [420, 280, 200, 100, 260, 100, 60]

fig = go.Figure(
    data=[
        go.Sankey(
            arrangement="snap",
            node=dict(
                pad=18,
                thickness=18,
                line=dict(color="rgba(0,0,0,0.2)", width=1),
                label=labels,
                color=[
                    "#4C78A8",
                    "#F58518",
                    "#E45756",
                    "#72B7B2",
                    "#54A24B",
                    "#EECA3B",
                    "#B279A2",
                    "#FF9DA6",
                ],
            ),
            link=dict(
                source=sources,
                target=targets,
                value=values,
                color=[
                    "rgba(76,120,168,0.45)",
                    "rgba(229,87,86,0.45)",
                    "rgba(114,183,178,0.45)",
                    "rgba(84,162,75,0.45)",
                    "rgba(245,133,24,0.45)",
                    "rgba(238,202,59,0.45)",
                    "rgba(178,121,162,0.45)",
                ],
            ),
        )
    ]
)

fig.update_layout(
    title=dict(text="Customer flow after visit (Plotly Sankey)", x=0.5, font=dict(size=18)),
    font=dict(size=12, color="#222"),
    margin=dict(l=10, r=10, t=40, b=10),
)

# Interactive HTML for sharing in a browser
fig.write_html("static/images/visualize/advanced/sankey-diagram.html", include_plotlyjs="cdn")

# Static image for docs (requires kaleido: pip install kaleido)
fig.write_image("static/images/visualize/advanced/sankey-diagram.png", scale=2)

plt.show()

Sankey diagram rendered with Plotly to show customer flow.

Reading tips #

  • Thicker bands represent larger flow, so you can spot major drop-offs quickly.
  • Follow connected branches to trace bottlenecks to a specific outcome (e.g., paid conversion).
  • If branching is too dense, focus on key paths to keep it readable.