Connecting flows in matplotlib sankey diagram
Asked Answered
L

2

4

I am using matplotlibs sankey functionality and have a problem with connecting two flows. Basically, I just want to connect the flow Qab,rekup to the end of the flow Qzu,rekup (see Screenshot).

Seems to be quite easy but I still haven't figured out how to manage this.

Here's the screenshot: https://www.dropbox.com/s/2satz9ryniy958v/Sankey.png?dl=0 enter image description here Here's the code:

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

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[],
                     title="Vereinfachtes Kraftwerksmodell")
sankey = Sankey(ax=ax, unit=None)
sankey.add(flows=[1.0, -0.3, -0.1, -0.1, -0.5],
           labels=['P$el$', 'Q$ab,vd$', 'P$vl,vd$', 'P$vl,mot$', ''],
           label='Laden',
           orientations=[0, -1, 1, 1, 0])
sankey.add(flows=[0.5, 0.1, 0.1, -0.1, -0.1, -0.1, -0.1, -0.3], fc='#37c959',
           label='Entladen',
           labels=['P$mech$', 'Q$zu,ex$', 'Q$zu,rekup$', 'P$vl,tb$', 'P$vl,gen$',         'Q$ab,tb$', 'Q$ab,rekup$', 'P$nutz$'],
           orientations=[0, -1, -1, 1, 1, -1, -1, 0], prior=0, connect=(4, 0))
sankey.add(flows=[-0.1, 0.1],
           label='Rekuperator',
           #labels=['bla'],
           orientations=[1,1], prior=1, connect=(2, 0))
diagrams = sankey.finish()
diagrams[-1].patch.set_hatch('/')
plt.legend(loc='lower right')
plt.show()

Does anyone have an idea?

Thanks in advance Cord

Linet answered 31/10, 2014 at 15:2 Comment(0)
T
4

I think I'm way too late, but here is the solution: You need to specify the pathlength for your first node, and tweak that manually to match up the smaller one.

https://static.mcmap.net/file/mcmap/ZG-Ab5ovKRkQZV0nc79QWRft/kAh1isL.png

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[],
                 title="Vereinfachtes Kraftwerksmodell")
sankey = Sankey(ax=ax, unit=None)
sankey.add(flows=[1.0, -0.3, -0.1, -0.1, -0.5],
       pathlengths = [0.5,0.06,0.5,0.5,0.375],
       labels=['P$el$', 'Q$ab,vd$', 'P$vl,vd$', 'P$vl,mot$', ''],
       label='Laden',
       orientations=[0, -1, 1, 1, 0])
sankey.add(flows=[0.5, 0.1, 0.1, -0.1, -0.1, -0.1, -0.1, -0.3], fc='#37c959',
       label='Entladen',
       labels=['P$mech$', 'Q$zu,ex$', 'Q$zu,rekup$', 'P$vl,tb$', 'P$vl,gen$',                'Q$ab,tb$', 'Q$ab,rekup$', 'P$nutz$'],
       orientations=[0, -1, -1, 1, 1, -1, -1, 0], prior=0, connect=(4, 0))
sankey.add(flows=[-0.1, 0.1],
       label='Rekuperator',
       #labels=['bla'],
       orientations=[1,1], prior=1, connect=(2, 0))
diagrams = sankey.finish()
diagrams[-1].patch.set_hatch('/')
plt.legend(loc='lower right')
plt.show()
Teachin answered 12/1, 2015 at 23:15 Comment(0)
R
6

I'm also ridiculously late, but there's a far simpler way of doing this than worrying about path lengths.

When you run a path backwards the orientation values are reversed, so -1 is up and 1 is down.

to fix your code all you need to do is change the Rekuperator sankey code to:

sankey.add(flows=[-0.1, 0.1],
       label='Rekuperator',
       #labels=['bla'],
       orientations=[-1,-1], prior=1, connect=(2, 0))

Producing this diagram: Producing this diagram

Robinia answered 3/8, 2017 at 9:26 Comment(2)
For anyone trying something like this, this is much easier than the accepted answerEthnology
Is the connection at "Qzu, rekup' just happenstance? It's technically not coded, correct? And occurs by virtue of the radii, pathlengths, trunklength. etc all being compatible...but this isn't guaranteed. Assuming I'm correct, is there a way to hard-code the discharge node for the Rekuperator?Benham
T
4

I think I'm way too late, but here is the solution: You need to specify the pathlength for your first node, and tweak that manually to match up the smaller one.

https://static.mcmap.net/file/mcmap/ZG-Ab5ovKRkQZV0nc79QWRft/kAh1isL.png

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[],
                 title="Vereinfachtes Kraftwerksmodell")
sankey = Sankey(ax=ax, unit=None)
sankey.add(flows=[1.0, -0.3, -0.1, -0.1, -0.5],
       pathlengths = [0.5,0.06,0.5,0.5,0.375],
       labels=['P$el$', 'Q$ab,vd$', 'P$vl,vd$', 'P$vl,mot$', ''],
       label='Laden',
       orientations=[0, -1, 1, 1, 0])
sankey.add(flows=[0.5, 0.1, 0.1, -0.1, -0.1, -0.1, -0.1, -0.3], fc='#37c959',
       label='Entladen',
       labels=['P$mech$', 'Q$zu,ex$', 'Q$zu,rekup$', 'P$vl,tb$', 'P$vl,gen$',                'Q$ab,tb$', 'Q$ab,rekup$', 'P$nutz$'],
       orientations=[0, -1, -1, 1, 1, -1, -1, 0], prior=0, connect=(4, 0))
sankey.add(flows=[-0.1, 0.1],
       label='Rekuperator',
       #labels=['bla'],
       orientations=[1,1], prior=1, connect=(2, 0))
diagrams = sankey.finish()
diagrams[-1].patch.set_hatch('/')
plt.legend(loc='lower right')
plt.show()
Teachin answered 12/1, 2015 at 23:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.