Skip to content

Sankey

The Sankey output widget displays how a numeric quantity moves between stages or categories. Each rectangle is a node, and each ribbon’s width represents its value. Flows are arranged from left to right.

Prepare the flows before constructing the widget. Sankey aggregates duplicate source-target pairs, but it does not derive flows from raw event data.

import pandas as pd
import mercury as mr
df = pd.DataFrame({
"source": ["Visitors", "Visitors", "Signup", "Signup"],
"target": ["Signup", "Left", "Paid", "Free"],
"value": [800, 200, 120, 680],
})
mr.Sankey(
df,
source="source",
target="target",
value="value",
)

You can also pass three-item tuples without creating a DataFrame:

mr.Sankey([
("Visitors", "Signup", 800),
("Visitors", "Left", 200),
("Signup", "Paid", 120),
("Signup", "Free", 680),
])
customer_flows = df.rename(columns={"value": "customers"})
mr.Sankey(
customer_flows,
source="source",
target="target",
value="customers",
height=500,
show_values=True,
value_format=",",
colors=[
"#3b82f6",
"#22c55e",
"#f59e0b",
"#ef4444",
],
)

colors accepts a list that cycles through nodes or a dictionary that assigns colors to specific node names:

mr.Sankey(
df,
colors={
"Visitors": "#2563eb",
"Signup": "#16a34a",
"Paid": "#f59e0b",
},
)

Links use their source node’s color. link_opacity controls ribbon transparency. Mercury theme colors are used when colors is omitted or a dictionary does not map every node.

Every row must contain a non-empty source, a non-empty target, and a finite, non-negative numeric value. Zero-value links are omitted, and duplicate links are summed.

Sankey diagrams currently require an acyclic graph. Self-links and cycles raise clear errors rather than producing an ambiguous layout.

source | target | value

Node names are always visible. Enable show_values to append each node’s total flow value to its label. The value is the larger of its total incoming and outgoing flow.

value_format accepts a standard Python numeric format specification:

mr.Sankey(df, show_values=True, value_format=",")

Nodes and ribbons include native browser tooltips. The SVG also includes accessible labels and a description, so values are not communicated through color alone.

The SVG fills the available width. Diagrams with many stages keep a readable minimum width and scroll horizontally inside narrow layouts such as mr.Columns(2).

ParameterDescriptionDefault
dataDataFrame, three-item tuples, or dictionariesrequired
sourceSource column or dictionary key"source"
targetTarget column or dictionary key"target"
valueNumeric value column or dictionary key"value"
colorsHex color list, node-color dictionary, or NoneNone
heightSVG layout height400
node_widthNode rectangle width16
node_paddingVertical spacing between nodes16
link_opacityRibbon opacity between zero and one0.35
show_valuesAppend values to node labelsFalse
value_formatPython numeric format specificationNone

The implementation is generated entirely in Python and rendered as SVG. It does not load Plotly, D3, or another JavaScript charting library.