Connecting flows in matplotlib sankey diagram

南楼画角 提交于 2019-12-04 09:04:30
jprawiharjo

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.

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()

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!