Connecting streams in a matplotlib sankey chart
I am using matplotlibs functions and have a problem connecting two streams. Basically, I just want to attach the stream to the end of the stream (see screenshot). sankey
Qab,rekup
Qzu,rekup
It seems to be quite easy, but I still haven't figured out how it can be done.
Here's a screenshot: https://www.dropbox.com/s/2satz9ryniy958v/Sankey.png?dl=0 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()
Anyone have an idea?
Thanks in advance Cord
source to share
I think I'm too late, but here is the solution: You need to specify the path length for your first node and manually adjust it to fit the smaller one.
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()
source to share
I'm ridiculously late too, but there is a much easier way to do this than worrying about path lengths.
When you run the path backwards, the orientation values ββare reversed, so -1 up and 1 down.
to fix your code, all you have to do is change your Rekuperator sankey code to:
sankey.add(flows=[-0.1, 0.1],
label='Rekuperator',
#labels=['bla'],
orientations=[-1,-1], prior=1, connect=(2, 0))
source to share