问题
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"
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