Home A Sankey Random Walk with Matplotlib
Post
Cancel

A Sankey Random Walk with Matplotlib

While looking into programmatically generating Sankey diagrams based on the results of discrete event simulations I made up a somewhat absurd programming exercise. Using matplotlib.sankey.Sankey you can draw a random walk as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import matplotlib.pyplot as plt
from matplotlib.sankey import Sankey
import numpy as np

np.random.seed(2018)

sankey = Sankey()

for i in range(100):
    sankey.add(
        flows=[1, -1],
        orientations=np.random.randint(-1, 2, size=2).tolist(),
        prior=None if not i else i - 1,
        connect=None if not i else (1, 0),
        labels=None,
    )

sankey.finish()

plt.axis("off")

plt.tight_layout()

plt.savefig('sankey_random_walk.png', dpi=300, transparent=True)

plt.close()

Here is the plot:

It is kind of pretty in my opinion.

This post is licensed under CC BY 4.0 by the author.

Plotting Ciw's Café Example As A Sankey Diagram Using Plotly

On Thoughts and Feelings Along With Their Meanings