Advanced examples
from ipysankeywidget import SankeyWidget
from ipywidgets import Layout
A convenience factory function
layout = Layout(width="300", height="200")
def sankey(margin_top=10, **value):
"""Show SankeyWidget with default values for size and margins"""
return SankeyWidget(layout=layout,
margins=dict(top=margin_top, 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)
SankeyWidget(layout=Layout(height='200', width='300'), links=[{'source': 'A', 'target': 'B', 'value': 1}, {'so…
rank_sets = [
{ 'type': 'same', 'nodes': ['C', 'D'] }
]
sankey(links=links, rank_sets=rank_sets)
SankeyWidget(layout=Layout(height='200', width='300'), links=[{'source': 'A', 'target': 'B', 'value': 1}, {'so…
order = [
['A'],
['D', 'B'],
['C'],
]
sankey(links=links, order=order)
SankeyWidget(layout=Layout(height='200', width='300'), links=[{'source': 'A', 'target': 'B', 'value': 1}, {'so…
order = [
[ [ ], ['A'], [], ],
[ ['B'], [ ], ['D'] ],
[ [ ], ['C'], [] ],
]
sankey(links=links, order=order)
SankeyWidget(layout=Layout(height='200', width='300'), links=[{'source': 'A', 'target': 'B', 'value': 1}, {'so…
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', 'direction': 'l'},
{'id': 'D', 'direction': 'l'},
]
sankey(links=links, nodes=nodes)
SankeyWidget(layout=Layout(height='200', width='300'), links=[{'source': 'A', 'target': 'B', 'value': 1}, {'so…
Variations:
nodes = [
{'id': 'C', 'direction': 'r'},
{'id': 'D', 'direction': 'l'},
]
sankey(links=links, nodes=nodes)
SankeyWidget(layout=Layout(height='200', width='300'), links=[{'source': 'A', 'target': 'B', 'value': 1}, {'so…
nodes = [
{'id': 'C', 'direction': 'l'},
{'id': 'D', 'direction': 'r'},
]
sankey(links=links, nodes=nodes)
SankeyWidget(layout=Layout(height='200', width='300'), links=[{'source': 'A', 'target': 'B', 'value': 1}, {'so…
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)
SankeyWidget(layout=Layout(height='200', width='300'), links=[{'source': 'A', 'target': 'B', 'value': 3, 'type…
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)
SankeyWidget(layout=Layout(height='200', width='300'), links=[{'source': 'A', 'target': 'B', 'value': 3, 'colo…
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)
SankeyWidget(layout=Layout(height='200', width='300'), links=[{'source': 'A', 'target': 'B', 'value': 3, 'colo…
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=[])
SankeyWidget(layout=Layout(height='200', width='300'), links=[{'source': 'A1', 'target': 'B', 'value': 1.5, 't…
sankey(links=links, align_link_types=True)
SankeyWidget(align_link_types=True, layout=Layout(height='200', width='300'), links=[{'source': 'A1', 'target'…
order = [
['A2', 'A1'],
['B'],
['C'],
]
sankey(links=links, align_link_types=True, order=order)
SankeyWidget(align_link_types=True, layout=Layout(height='200', width='300'), links=[{'source': 'A1', 'target'…
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])
VBox(children=(Button(description='Swap', style=ButtonStyle()), SankeyWidget(layout=Layout(height='200', width…
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=30)
SankeyWidget(groups=[{'id': 'G', 'title': 'Group', 'nodes': ['C', 'D']}], layout=Layout(height='200', width='3…
Link labels show the numeric value of the link on the diagram:
sankey(links=links, linkLabelFormat='.1f')
SankeyWidget(layout=Layout(height='200', width='300'), linkLabelFormat='.1f', links=[{'source': 'A', 'target':…
By default the labels for small links are hidden, but you can customize this using linkLabelMinWidth
:
links[2]['value'] = 0.1
links[1]['value'] = 2.9
sankey(links=links, linkLabelFormat='.1f')
SankeyWidget(layout=Layout(height='200', width='300'), linkLabelFormat='.1f', links=[{'source': 'A', 'target':…
sankey(links=links, linkLabelFormat='.1f', linkLabelMinWidth=4)
SankeyWidget(layout=Layout(height='200', width='300'), linkLabelFormat='.1f', linkLabelMinWidth=4.0, links=[{'…
Warning: this feature is experimental and may be changed.
links[0]['marker'] = 2.5
sankey(links=links)
SankeyWidget(layout=Layout(height='200', width='300'), links=[{'source': 'A', 'target': 'B', 'value': 3, 'type…
Warning: this feature is experimental and may be changed.
links = [
{'source': 'A', 'target': 'B', 'value': 3, 'type': 'x', 'info_html': 'Hi!'},
{'source': 'B', 'target': 'C', 'value': 2, 'type': 'y', 'info_html': 'B <b>to</b> C'},
{'source': 'B', 'target': 'D', 'value': 1, 'type': 'z'},
]
sankey(links=links, show_link_info_html=True)
SankeyWidget(layout=Layout(height='200', width='300'), links=[{'source': 'A', 'target': 'B', 'value': 3, 'type…
The d3-sankey-diagram layout algorithm can be bypassed completely by specifying coordinates for the nodes directly. This must be activated by the node_position_attr
option.
links = [
{'source': 'A', 'target': 'B', 'value': 30},
{'source': 'B', 'target': 'C', 'value': 20},
{'source': 'B', 'target': 'D', 'value': 10},
]
nodes = [
{'id': 'A', 'position': [0, 50]},
{'id': 'B', 'position': [100, 50]},
{'id': 'C', 'position': [200, 30]},
{'id': 'D', 'position': [200, 100]},
]
w = sankey(
links=links,
nodes=nodes,
node_position_attr='position'
)
w
SankeyWidget(layout=Layout(height='200', width='300'), links=[{'source': 'A', 'target': 'B', 'value': 30}, {'s…
The positions are in display coordinates, within the margins specified. The scale is set to 1 by default, if not specified. When node positions are specified manually, they are not affected by the scale -- only the width of the lines is scaled.
# Try changing this
w.scale = 2
# Try changing this
w.nodes[0]['position'] = [50, 50]
w.send_state()
# w.node_position_attr = None