0

I would like to create sankey diagram showing two seprated flows? Flow named L and P. Like one image below (It's only example image for tests). enter image description here

How to do it with matplotlib sankey? My test code below

    sankey = Sankey()
    
    sankey.add(flows=[200, -50, -100, -50],
               orientations=[0, 1, 0, -1],
               labels=['L', 'L1', 'L2', 'L3'],
               trunklength=200,
               )

    sankey.add(flows=[200, -50, -100, -50],
               orientations=[0, 1, 0, -1],
               labels=['P', 'P1', 'P2', 'P3'],
               trunklength=200,
               rotation=180,
               )
    
    sankey.finish()
    plt.savefig('tests/TestPloting/Sankey.png')

produces diffrent image. Created image looks like that : enter image description here

s.paszko
  • 633
  • 1
  • 7
  • 21

2 Answers2

1

Sankey() has on optional parameter ax= to put the diagram into a given subplot. You can create two subplots below each other via e.g. plt.subplots(nrows=2, figsize=(5, 12)), or next to each other using ncols=2 with an adequate figsize.

import matplotlib.pyplot as plt
from matplotlib.sankey import Sankey

fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(12, 5))

sankey = Sankey(ax=ax1)
sankey.add(flows=[200, -50, -100, -50],
           orientations=[0, 1, 0, -1],
           labels=['L', 'L1', 'L2', 'L3'],
           trunklength=200,
           )
sankey.finish()
ax1.set_xticks([])  # remove xticks
ax1.set_yticks([])

sankey = Sankey(ax=ax2)
sankey.add(flows=[200, -50, -100, -50],
           orientations=[0, 1, 0, -1],
           labels=['P', 'P1', 'P2', 'P3'],
           trunklength=200,
           rotation=180,
           facecolor='C1'
           )
sankey.finish()
ax2.set_xticks([])
ax2.set_yticks([])
plt.tight_layout()
plt.show()

two separate Sankey diagrams

JohanC
  • 71,591
  • 8
  • 33
  • 66
0

The closest solution to my problem is using Sankey with extra but non-visible parts of diagram. To add non visible sankey flow we can use add() method with optional parameter visible=False. This flow unfortunately can't have zero flow input and output. To omit this condition i have used value 1e-6 as almost nothing flow. Here is full code :

from matplotlib.sankey import Sankey
import matplotlib.pyplot as plt


def test_plot_sankey():
    ''' Test case.'''
    sankey = Sankey()

    # Left side
    sankey.add(flows=[100 +1e-6, -1e-6, -100],
               orientations=[0, 1, 0],
               trunklength=200,
               labels=['L'],
               )

    # Turn 90deg
    sankey.add(flows=[1e-6, -1e-6],
               orientations=[0, 1],
               trunklength=100,
               visible=False,
               prior=0,
               connect=(1,0)
               )

    # Right side
    sankey.add(flows=[1e-6, 100, -100 - 1e-6],
               orientations=[0, 0, 0],
               trunklength=200,
               labels=['R'],
               prior=1,
               connect=(1,0)
               )


    sankey.finish()
    plt.savefig('tests/TestPloting/Sankey.png')

Image with results. (Yes i have intentionally simplified 3 output flows to only one, for testing!)

Image results (Yes i intentionally simplified 3 output flows to only one, for testing purporses).

s.paszko
  • 633
  • 1
  • 7
  • 21