Fundamentals 5 min read

Creating Sankey Diagrams in Python with Matplotlib and Pyecharts

This tutorial explains what Sankey diagrams are, shows their main forms, and provides step‑by‑step Python code using Matplotlib and Pyecharts to generate both basic and advanced Sankey visualizations, including tips on data preparation and styling.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Creating Sankey Diagrams in Python with Matplotlib and Pyecharts

A Sankey diagram (also called a Sankey energy flow diagram) is a specialized flow chart where the width of each branch corresponds to the magnitude of the flow, making it useful for visualizing energy, material composition, or financial data.

The diagram mainly appears in two styles, which are illustrated below.

Below is a Matplotlib example that builds a simple Sankey chart:

import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
from matplotlib.sankey import Sankey

plt.rcParams['figure.figsize'] = (7.0, 5.0)
plt.rcParams['savefig.dpi'] = 100  # image resolution
plt.rcParams['figure.dpi'] = 100

flows = [0.2, 0.1, 0.4, 0.3, -0.6, -0.05, -0.15, -0.2]
labels = ['', '', '', '', 'family', 'trip', 'education', 'sport']
orientations = [1, 1, 0, -1, 1, -1, 1, 0]

sankey = Sankey()
sankey.add(flows=flows,
           labels=labels,
           orientations=orientations,
           color='c',
           fc='lightgreen',
           patchlabel='Life Cost',
           alpha=0.7)

diagrams = sankey.finish()
diagrams[0].texts[4].set_color('r')
diagrams[0].texts[4].set_weight('bold')
diagrams[0].texts[4].set_fontsize(15)
diagrams[0].text.set_fontsize(20)
diagrams[0].text.set_fontweight('bold')

plt.title('The Flow Chart of Daily Cost')
plt.show()

The resulting chart looks like this:

A more visually striking Sankey diagram, similar to the one shown in an ECharts tutorial, is also presented:

To create Sankey diagrams with Python, you can also use the Pyecharts library. The following code demonstrates a vertical Sankey chart with custom colors and interactive tooltips:

from pyecharts import options as opts
from pyecharts.charts import Sankey

colors = ["#67001f","#b2182b","#d6604d","#f4a582","#fddbc7",
          "#d1e5f0","#92c5de","#4393c3","#2166ac","#053061"]

nodes = [
    {"name": "a"},
    {"name": "b"},
    {"name": "a1"},
    {"name": "b1"},
    {"name": "c"},
    {"name": "e"},
]

links = [
    {"source": "a", "target": "a1", "value": 5},
    {"source": "e", "target": "b", "value": 3},
    {"source": "a", "target": "b1", "value": 3},
    {"source": "b1", "target": "a1", "value": 1},
    {"source": "b1", "target": "c", "value": 2},
    {"source": "b", "target": "c", "value": 1},
]

c = (
    Sankey()
    .set_colors(colors)
    .add(
        "sankey",
        nodes=nodes,
        links=links,
        pos_bottom="10%",
        focus_node_adjacency="allEdges",
        orient="vertical",
        linestyle_opt=opts.LineStyleOpts(opacity=0.2, curve=0.5, color="source"),
        label_opts=opts.LabelOpts(position="top"),
    )
    .set_global_opts(
        title_opts=opts.TitleOpts(title="Sankey-Vertical"),
        tooltip_opts=opts.TooltipOpts(trigger="item", trigger_on="mousemove"),
    )
    .render("sankey_vertical.html")
)

The chart produced by this script appears as follows:

When constructing Sankey diagrams, ensure that your data is organized in a source → target → value format so that the relationships between adjacent categories can be correctly visualized.

Disclaimer: This article is compiled from online sources; all copyrights belong to the original authors. If any information is incorrect or infringes rights, please contact us for removal or authorization.

PythonData VisualizationMatplotlibpyechartsSankey Diagram
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.