<jupyter_start><jupyter_text>IntroductionA [**Sankey diagram**](https://www.data-to-viz.com/graph/sankey.html) is a visualisation technique that allows to display flows. Several entities (nodes) are represented by rectangles or text. Their links are represented with arrow or arcs that have a width proportional to the importance of the flow.The [pySankey](https://github.com/anazalea/pySankey) library, which is based on Matplotlib, makes it extremely easy to obtain Sankey diagrams in Python. This post is based on the library's documentation and aims to explain how to obtain Sankey diagrams with the pySankey library.The `pySankey` library can be installed with `pip install pysankey`, but note you need to use `pySankey` instead of `pysankey` when importing the library or something from it.<jupyter_code>import pandas as pd

# Import the sankey function from the sankey module within pySankey
from pySankey.sankey import sankey<jupyter_output><empty_output><jupyter_text>Basic Sankey diagramLet's import the `fruits.txt` dataset that comes with the library. Here we download it from the [github repository](https://raw.githubusercontent.com/anazalea/pySankey/master/pysankey/fruits.txt).The dataset has 2 columns only. Each row describes a connection, with the origin in the first column and the destination in the second. If a connection has several occurences in the dataset (the same row appears many times), its weight will be higher and the connection on the diagram will be bigger.The `sankey()` function is used to draw the diagram. It takes at least 2 arguments as input: the origin and destination columns:<jupyter_code>url = "https://raw.githubusercontent.com/anazalea/pySankey/master/pysankey/fruits.txt"
df = pd.read_csv(url, sep=" ", names=["true", "predicted"])

colors = {
    "apple": "#f71b1b",
    "blueberry": "#1b7ef7",
    "banana": "#f3f71b",
    "lime": "#12e23f",
    "orange": "#f78c1b"
}

sankey(df["true"], df["predicted"], aspect=20, colorDict=colors, fontsize=12)<jupyter_output><empty_output><jupyter_text>Dataset with weightsIt's also possible to use weights. The following diagram is based on the [customer-goods.csv](https://raw.githubusercontent.com/anazalea/pySankey/master/pysankey/customers-goods.csv) data from the pySankey library. This time each connection has only 1 row in the dataset, but its weight is explicitely provided in a column called `revenue`. We can provide this column to the `leftWeight` and `rightWeight` argument to draw the connections with the according sizes.<jupyter_code>url = "https://raw.githubusercontent.com/anazalea/pySankey/master/pysankey/customers-goods.csv"
df = pd.read_csv(url, sep=",")

sankey(
    left=df["customer"], right=df["good"], 
    leftWeight= df["revenue"], rightWeight=df["revenue"], 
    aspect=20, fontsize=20
)<jupyter_output><empty_output><jupyter_text>Save the figure (`.png`)You need [matplotlib](https://www.python-graph-gallery.com/matplotlib/) if you want to save the diagram with a custom size:<jupyter_code>import matplotlib.pyplot as plt

# Create Sankey diagram again
sankey(
    left=df["customer"], right=df["good"], 
    leftWeight= df["revenue"], rightWeight=df["revenue"], 
    aspect=20, fontsize=20
)

# Get current figure
fig = plt.gcf()

# Set size in inches
fig.set_size_inches(6, 6)

# Set the color of the background to white
fig.set_facecolor("w")

# Save the figure
fig.savefig("customers-goods.png", bbox_inches="tight", dpi=150)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10542947428899742, 'fraction_numerical': 0.010341855788566504, 'mean_word_length': 5.520599250936329, 'pattern_counts': {'":': 5, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 7, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '308753', 'license': '0BSD', 'n_tokens_mistral': 1097, 'n_tokens_neox': 1062, 'n_words': 367, 'path': 'src/notebooks/basic-sankey-diagram-with-pysankey.ipynb', 'repo_name': 's-lasch/The-Python-Graph-Gallery', 'size': '3481'}