# 6.2.5 Sankey Chart

Sankey diagrams visualize the contributions to flow by defining `source` to represent the source node,  `target` for the target node, `value` to set the flow volume, and label that shows the node name.

if a flow is twice as wide it represents double the quantity. Flows in the diagram can show e.g. energy, materials, water, or costs.

{% hint style="info" %}

* The link is a dictionary containing data about the connections we want to draw.
* The source and target are lists of indexes for the nodes Plotly will connect.
* The value is a list of numbers that will define the width of these connections.
  {% endhint %}

### 1. Basic Sankey Chart

```
label = ["ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE"]
source = [0, 0, 1, 1, 0]
target = [2, 3, 3, 5, 4]
value = [8, 3, 2, 8, 4]

link = dict(source = source, target = target, value = value)
node = dict(label = label, pad=50, thickness=5)
data = go.Sankey(link = link, node=node)

fig = go.Figure(data)
fig.show()
```

![](https://998709212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB-ky7fVqjeXA6EcAbW%2F-MDAtzBOnlF6NZqODBx9%2F-MDB1WM4rJLOon1OMImW%2FScreenshot%202020-07-26%20at%2018.46.21.png?alt=media\&token=f98d0304-1ca0-4d73-ad76-7f4665902af2)

### 2. Customized Sankey Chart

```
label = ["ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE"]
source = [0, 0, 1, 1, 0]
target = [2, 3, 3, 5, 4]
value = [8, 3, 2, 8, 4]

# add color list
colors = ['#F27420','#4994CE','#FABC13','#7FC241','#EC2272']

# data to dict, dict to sankey
link = dict(source = source, target = target, value = value,color = colors)
node = dict(label = label, pad=50, thickness=5)
data = go.Sankey(link = link, node=node)
# plot
fig = go.Figure(data)
fig.update_layout(height=600,font_size = 14) # set figure and font size
fig.show()
```

![](https://998709212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB-ky7fVqjeXA6EcAbW%2F-MDAtzBOnlF6NZqODBx9%2F-MDB1wcxv94WgSukdfXN%2Fsankey.gif?alt=media\&token=19ac9a27-34f4-4a97-be47-ed561f7bb0b2)

### 3. Vertical Sankey Chart

It's very easy to make the chart in vertical, just address **`orientation = 'v'`**. However,  the horizontal Sankey chart is in common use.

```
link = dict(source = source, target = target, value = value,color = colors)
node = dict(label = label, pad=50, thickness=5)
data = go.Sankey(link = link, node=node, orientation = 'v')
# plot
fig = go.Figure(data)
```

![](https://998709212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB-ky7fVqjeXA6EcAbW%2F-MDAtzBOnlF6NZqODBx9%2F-MDB2KJwsQZDYgl8M-jD%2FScreenshot%202020-07-26%20at%2018.45.12.png?alt=media\&token=294d4b46-2835-4369-a9b5-7fbc57c3c83a)
