Sankey charts#

Note

Requires the sankey module:

easychart.config.scripts.append("https://code.highcharts.com/modules/sankey.js")
easychart.config.save()

See section on extensions for more details.

import easychart

data = [
    ["Brazil", "Portugal", 5],
    ["Brazil", "France", 1],
    ["Canada", "Portugal", 1],
    ["Canada", "France", 5],
    ["Mexico", "Portugal", 1],
    ["Mexico", "France", 1],
    ["USA", "Portugal", 1],
    ["USA", "France", 1],
    ["Portugal", "Angola", 2],
    ["Portugal", "Senegal", 1],
    ["France", "Angola", 1],
    ["France", "Senegal", 3],
    ["Spain", "Senegal", 1],
    ["Spain", "Morocco", 3],
    ["England", "Morocco", 2],
    ["England", "South Africa", 7],
    ["South Africa", "China", 5],
    ["Angola", "Japan", 3],
    ["Senegal", "India", 1],
    ["Senegal", "Japan", 3],
    ["Mali", "India", 1],
    ["Morocco", "India", 1],
    ["Morocco", "Japan", 3],
]

chart = easychart.new(type="sankey")
chart.title = "Sankey diagram"
chart.subtitle = (
    "Flow diagram in which the width of the arrows is proportional to the flow rate"
)
chart.plot(data, keys=["from", "to", "weight"])
chart

Setting ribbon colors#

import easychart

data = [
    ["A", "D", 9, "#BCD4DE"],  # gray
    ["A", "C", 2, "#FFD675"],  # light yellow
    ["D", "E", 4, "#BCD4DE"],  # gray
    ["D", "F", 4, "#BCD4DE"],  # gray
    ["B", "C", 4, "#FFD675"],  # light yellow
    ["B", "D", 4, "#BCD4DE"],  # gray
    ["C", "E", 3, "#F7BA2D"],  # darker yellow
    ["C", "F", 3, "#BCD4DE"],  # gray
]

chart = easychart.new(type="sankey")
chart.plot(data, keys=["from", "to", "weight", "color"])
chart

Setting ribbon and node colors#

import easychart

data = [
    ["A", "D", 9, "#BCD4DE"],  # gray
    ["A", "C", 2, "#FFD675"],  # light yellow
    ["D", "E", 4, "#BCD4DE"],  # gray
    ["D", "F", 4, "#BCD4DE"],  # gray
    ["B", "C", 4, "#FFD675"],  # light yellow
    ["B", "D", 4, "#BCD4DE"],  # gray
    ["C", "E", 3, "#F7BA2D"],  # darker yellow
    ["C", "F", 3, "#BCD4DE"],  # gray
]

nodes = [
    {"id": "A", "color": "#457b9d"},  # blue
    {"id": "B", "color": "#2a9d8f"},  # green
    {"id": "C", "color": "#e9c46a"},  # yellow
    {"id": "D", "color": "#f4a261"},  # orange
    {"id": "E", "color": "#e76f51"},  # red
    {"id": "F", "color": "#1d3557"},  # night
]

chart = easychart.new(type="sankey")
chart.plot(data, keys=["from", "to", "weight", "color"], nodes=nodes)
chart

Setting data labels#

import easychart

labels = {
    "format": "{point.from} -> {point.to} {point.weight}km",
    "nodeFormat": "Node {point.name}",
}

data = [
    ["A", "D", 9],
    ["A", "C", 2],
    ["D", "E", 4],
    ["D", "F", 4],
    ["B", "C", 4],
    ["B", "D", 2],
    ["C", "E", 3],
    ["C", "F", 3],
]

chart = easychart.new(type="sankey")
chart.plot(data, keys=["from", "to", "weight"], labels=labels)
chart

Setting filtered edge labels with node labels#

import easychart

labels = [
    {
        "format": "{point.from} -> {point.to} {point.weight}km",
        "filter": {"property": "weight", "operator": ">", "value": 3},
    },
    {"nodeFormat": "Point {point.name}"},
]

data = [
    ["A", "D", 9],
    ["A", "C", 2],
    ["D", "E", 4],
    ["D", "F", 4],
    ["B", "C", 4],
    ["B", "D", 2],
    ["C", "E", 3],
    ["C", "F", 3],
]

chart = easychart.new(type="sankey")
chart.plot(data, keys=["from", "to", "weight"], labels=labels)
chart