Connecting more than two systems in a sankey diagram using matplotlib gives me misalignment

£可爱£侵袭症+ 提交于 2021-02-07 18:14:54

问题


I am trying to make a slightly complex sankey diagram using matplotlib, It is supposed to be dynamical in the sense that I should be able to change the values of the flows and that all connections should stay connected. This means that I cannot manually adjust the path-lengths like suggested in this example. To familiarise myself with the explicit and implicit connections I tried to continue building from this example of two systems connected together with only one explicit connection. With this example you can change the values of the flows and things stay connected correctly. So I tried to add a fourth system that goes from system 1 to 2, but I can't seem to get the implicit connection to work. Please see the code and output below.

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


fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[], title="Two Systems")
flows = [0.3, 0.1, 0.40, -0.20, -0.4, -0.2]
sankey = Sankey(ax=ax, unit=None, radius=0.15, )


sankey.add(flows=flows, label='one',
           orientations=[-1, 1, 0, 1, 0, -1])
sankey.add(flows=[-0.3, 0.2, 0.1], label='two',
           orientations=[-1, -1, 0], prior=0, connect=(0, 0))
sankey.add(flows=[-0.1,-0.1,0.2], label='three',
           orientations=[1,0,1], prior=0,connect=(1, 0))
sankey.add(flows=[0.4,-0.1,-0.3], label='four',
           orientations=[-1,-1,0], prior=0,connect=(4, 0))
diagrams = sankey.finish()
diagrams[-1].patch.set_hatch('/')
plt.legend(loc='best')


plt.show()
print "Test"

Misaligned system in sankey diagram

Any hints or tips to make a dynamic sankey diagram with several connected systems would be greatly appreciated.


回答1:


My thought is that you can't know the position of the respective tips until the diagram has been finalized, so you'll need to finish the diagram once, then get the tip locations for flow 2 on patch 2 (orange) and flow 1 on patch 4 (red). The difference in the tip locations (x_adj,y_adj) is the adjustment that needs to be made to the trunk length of patch 4 (to correct the vertical direction) and the pathlength of flow 2 on patch 2.

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


x_adj,y_adj = 0,0
for _ in range(2):
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[], title="Two Systems")
    sankey = Sankey(ax=ax, unit=None, radius=0.15, )
    sankey.add(flows=[0.3, 0.1, 0.40, -0.20, -0.4, -0.2],
               label='one',
               orientations=[-1, 1, 0, 1, 0, -1])
    sankey.add(flows=[-0.3, 0.2, 0.1], label='two',
               pathlengths = [0.5,0.5,0.5- x_adj],
               orientations=[-1, -1, 0], prior=0, connect=(0, 0))
    sankey.add(flows=[-0.1,-0.1,0.2], label='three',
               orientations=[1,0,1], prior=0,connect=(1, 0))
    sankey.add(flows=[0.4,-0.1,-0.3], label='four', trunklength=1. - y_adj,
               orientations=[-1,-1,0], prior=0,connect=(4, 0))
    diagrams = sankey.finish()
    print(x_adj,y_adj)
    x_adj,y_adj = diagrams[1].tips[2] - diagrams[3].tips[1]
diagrams[-1].patch.set_hatch('/')
plt.legend(loc='best')
plt.show()


来源:https://stackoverflow.com/questions/51650775/connecting-more-than-two-systems-in-a-sankey-diagram-using-matplotlib-gives-me-m

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