Skip to content

Creating a Sankey Diagram in Python

[

Sankey Diagram in Python

Sankey diagrams are a powerful visualization tool that display flows of information or quantities between different nodes. In Python, there are various libraries that can be used to create Sankey diagrams, including matplotlib and plotly. In this tutorial, we will explore how to create Sankey diagrams using both of these libraries.

Table of Contents

  1. Sankey Diagram with Matplotlib
  2. Sankey Diagram with Plotly

Sankey Diagram with Matplotlib

Matplotlib is a popular library for creating static visualizations in Python. To create a Sankey diagram with Matplotlib, follow the steps below:

  1. Install the matplotlib library if you haven’t already: pip install matplotlib

  2. Import the required libraries:

import matplotlib.pyplot as plt
from matplotlib.sankey import Sankey
  1. Define the input data for the Sankey diagram. This includes the labels of the nodes and the flows between them:
labels = ['Node A', 'Node B', 'Node C', 'Node D', 'Node E']
flows = [ # Input values for the flows
[0, 1, 100], # Flow from Node A to Node B with a value of 100
[0, 2, 50], # Flow from Node A to Node C with a value of 50
[2, 3, 30], # Flow from Node C to Node D with a value of 30
[2, 4, 20] # Flow from Node C to Node E with a value of 20
]
  1. Create the Sankey diagram using the Sankey class:
fig, ax = plt.subplots()
sankey = Sankey(ax=ax)
sankey.add(flows=flows, labels=labels)
sankey.finish()
  1. Customize the appearance of the Sankey diagram by adding labels and colors:
sankey.add(flows=flows, labels=labels, orientations=[-1, 1, 0, -1, 1])
diagrams = sankey.finish()
diagrams[-1].texts[-1].set_color('r')
  1. Display the Sankey diagram:
plt.show()

Sankey Diagram with Plotly

Plotly is a powerful library for creating interactive visualizations in Python. To create a Sankey diagram with Plotly, follow the steps below:

  1. Install the plotly library if you haven’t already: pip install plotly

  2. Import the required libraries:

import plotly.graph_objects as go
  1. Define the input data for the Sankey diagram. This includes the labels of the nodes and the flows between them:
labels = ['Node A', 'Node B', 'Node C', 'Node D', 'Node E']
source = [0, 0, 2, 2]
target = [1, 2, 3, 4]
value = [100, 50, 30, 20]
  1. Create the Sankey diagram using the Sankey class from plotly.graph_objects:
fig = go.Figure(data=[go.Sankey(
node=dict(
pad=15,
thickness=20,
line=dict(color="black", width=0.5),
label=labels
),
link=dict(
source=source,
target=target,
value=value
)
)])
  1. Customize the appearance of the Sankey diagram:
fig.update_layout(
title_text="Sankey Diagram",
font_size=12,
height=600,
width=800
)
  1. Display the Sankey diagram:
fig.show()

In conclusion, creating Sankey diagrams in Python is made easy with the help of libraries like matplotlib and plotly. Whether you prefer static or interactive visualizations, you can effectively represent flows of information or quantities using Sankey diagrams.