[ Simple example](./Simple example.ipynb)
Advanced examples
[ Linking and Layout](./Linking and Layout.ipynb)
[ Exporting Images](./Exporting Images.ipynb)
from ipysankeywidget import SankeyWidget
from ipywidgets import Layout
A convenience factory function
layout = Layout(width="300", height="200")
def sankey(**value):
"""Show SankeyWidget with default values for size and margins"""
return SankeyWidget(layout=layout,
margins=dict(top=10, bottom=0, left=30, right=60),
**value)
You can adjust the left-right placement of nodes by putting them in rank sets: all nodes in the same set end up with the same rank.
links = [
{'source': 'A', 'target': 'B', 'value': 1},
{'source': 'B', 'target': 'C', 'value': 1},
{'source': 'A', 'target': 'D', 'value': 1},
]
sankey(links=links)
rank_sets = [
{ 'type': 'same', 'nodes': ['C', 'D'] }
]
sankey(links=links, rank_sets=rank_sets)
order = [
['A'],
['D', 'B'],
['C'],
]
sankey(links=links, order=order)
order = [
[ [ ], ['A'], [], ],
[ ['B'], [ ], ['D'] ],
[ [ ], ['C'], [] ],
]
sankey(links=links, order=order)
Most nodes are assumed to link from left to right, but sometimes there are return links which should be shown from right to left.
links = [
{'source': 'A', 'target': 'B', 'value': 1},
{'source': 'B', 'target': 'C', 'value': 1},
{'source': 'C', 'target': 'D', 'value': 1},
{'source': 'A', 'target': 'E', 'value': 0.5},
]
nodes = [
{'id': 'C', 'backwards': True},
{'id': 'D', 'backwards': True},
]
sankey(links=links, nodes=nodes)
Variations:
nodes = [
{'id': 'C', 'backwards': False},
{'id': 'D', 'backwards': True},
]
sankey(links=links, nodes=nodes)
nodes = [
{'id': 'C', 'backwards': True},
{'id': 'D', 'backwards': False},
]
sankey(links=links, nodes=nodes)
By default, the links are coloured according to their type:
links = [
{'source': 'A', 'target': 'B', 'value': 3, 'type': 'x'},
{'source': 'B', 'target': 'C', 'value': 2, 'type': 'y'},
{'source': 'B', 'target': 'D', 'value': 1, 'type': 'z'},
]
sankey(links=links)
You can also set the colours directly:
links = [
{'source': 'A', 'target': 'B', 'value': 3, 'color': 'steelblue'},
{'source': 'B', 'target': 'C', 'value': 2, 'color': '#aaa'},
{'source': 'B', 'target': 'D', 'value': 1, 'color': 'goldenrod'},
]
sankey(links=links)
Process titles default to their ids, but can be overridden. There are also one built-in alternative "style" of node:
process
is drawn with a thicker linenodes = [
{'id': 'B', 'title': 'Middle node', 'style': 'process' },
]
sankey(links=links, nodes=nodes)
Of course, you can also use CSS to adjust the styling:
%%html
<style>
.sankey .node {
font-style: italic;
}
</style>
links = [
{'source': 'A1', 'target': 'B', 'value': 1.5, 'type': 'x'},
{'source': 'A1', 'target': 'B', 'value': 0.5, 'type': 'y'},
{'source': 'A2', 'target': 'B', 'value': 0.5, 'type': 'x'},
{'source': 'A2', 'target': 'B', 'value': 1.5, 'type': 'y'},
{'source': 'B', 'target': 'C', 'value': 2.0, 'type': 'x'},
{'source': 'B', 'target': 'C', 'value': 2.0, 'type': 'y'},
]
sankey(links=links, nodes=[])
sankey(links=links, align_link_types=True)
order = [
['A2', 'A1'],
['B'],
['C'],
]
sankey(links=links, align_link_types=True, order=order)
from ipywidgets import Button, VBox
links = [
{'source': 'A', 'target': 'B', 'value': 1},
{'source': 'B', 'target': 'C', 'value': 1},
{'source': 'A', 'target': 'D', 'value': 1},
]
order = [
['A'],
['D', 'B'],
['C'],
]
s = sankey(links=links, order=order)
def swap(x):
global order
order = [list(reversed(o)) for o in order]
s.order = order
b = Button(description='Swap')
b.on_click(swap)
VBox([b, s])
links = [
{'source': 'A', 'target': 'B', 'value': 3, 'type': 'x'},
{'source': 'B', 'target': 'C', 'value': 2, 'type': 'y'},
{'source': 'B', 'target': 'D', 'value': 1, 'type': 'z'},
]
groups = [
{'id': 'G', 'title': 'Group', 'nodes': ['C', 'D']}
]
sankey(links=links, nodes=[], groups=groups, margin_top=50)