Skip to main content

Make simple, pretty Sankey Diagrams as a matplotlib object

Project description


Uses matplotlib to create simple Sankey diagrams flowing from left to right.

A fork of a fork of pySankey.

PyPI version Build Status Coverage Status License: GPL v3


Requires python-tk (for python 2.7) or python3-tk (for python 3.x) install with apt-get or your package manager.

You can install the other requirements with:

    pip install -r requirements.txt


With fruits.txt :

true predicted
0 blueberry orange
1 lime orange
2 blueberry lime
3 apple orange
... ... ...
996 lime orange
997 blueberry orange
998 orange banana
999 apple lime

1000 rows × 2 columns

You can generate a sankey's diagram with this code (colorDict is optional):

import pandas as pd
import matplotlib.pyplot as plt

from pysankey import sankey

df = pd.read_csv(
    'pysankey/tests/fruits.txt', sep=' ', names=['true', 'predicted']
colorDict = {

ax = sankey(
    df['true'], df['predicted'], aspect=20, colorDict=colorDict,
) # to display
plt.savefig('fruit.png', bbox_inches='tight') # to save

With customer-goods.csv :


You could also weight:

import pandas as pd
import matplotlib.pyplot as plt

from pysankey import sankey

df = pd.read_csv(
    'pysankey/tests/customers-goods.csv', sep=',',
    names=['id', 'customer', 'good', 'revenue']
weight = df['revenue'].values[1:].astype(float)

ax = sankey(
      left=df['customer'].values[1:], right=df['good'].values[1:],
      rightWeight=weight, leftWeight=weight, aspect=20, fontsize=20
) # to display
plt.savefig('customers-goods.png', bbox_inches='tight') # to save

Similar to seaborn, you can pass a matplotlib Axes to sankey function with the keyword ax=:

import pandas as pd
import matplotlib.pyplot as plt

from pysankey import sankey

df = pd.read_csv(
        sep=' ', names=['true', 'predicted']
colorDict = {
    'apple': '#f71b1b',
    'blueberry': '#1b7ef7',
    'banana': '#f3f71b',
    'lime': '#12e23f',
    'orange': '#f78c1b'

ax1 = plt.axes()

ax1 = sankey(
      df['true'], df['predicted'], aspect=20, colorDict=colorDict,
      fontsize=12, ax=ax1

Important informations

Use of figureName, closePlot and figSize in sankey() has been removed. This is done so matplotlib is used more transparently as this [issue] suggested ( on the original github repo.

Now, sankey() does less of the customization and let the user do it to their liking by returning a matplotlib Axes object, which mean the user also has access to the Figure to customise. Then they can choose what to do with it - showing it, saving it with much more flexibility.

Recommended changes to your code from pySankey

  • To save a figure, after sankey(), one can simply do:
  plt.savefig("<figureName>.png", bbox_inches="tight", dpi=150)
  • To display the diagram, simply do after sankey().

  • You can modify the sankey size by changing the one from the matplotlib figure.

  • It is possible to modify the diagram font looks, for example, add the following lines before calling sankey() :

      plt.rc("text", usetex=False)
      plt.rc("font", family="serif")

Package development


pylint pysankey


python -m unittest


coverage run -m unittest
coverage html
# Open htmlcov/index.html in a navigator

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for pySankey2, version 2.0.1
Filename, size File type Python version Upload date Hashes
Filename, size pySankey2-2.0.1-py3-none-any.whl (21.8 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size pySankey2-2.0.1.tar.gz (17.4 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page