使用 matplotlib 在桑基图中连接两个以上的系统使我错位 [英] Connecting more than two systems in a sankey diagram using matplotlib gives me misalignment

查看:30
本文介绍了使用 matplotlib 在桑基图中连接两个以上的系统使我错位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 matplotlib 制作一个稍微复杂的桑基图,它应该是动态的,因为我应该能够更改流的值并且所有连接都应该保持连接.这意味着我无法像示例中所建议的那样手动调整路径长度.为了熟悉显式和隐式连接,我尝试从两个系统的 this 示例继续构建仅通过一个显式连接连接在一起.通过此示例,您可以更改流的值,并使事物保持正确连接.因此,我尝试添加从系统 1 到系统 2 的第四个系统,但我似乎无法使隐式连接起作用.请参见下面的代码和输出.

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.

推荐答案

我的想法是,在图完成之前,您无法知道各个提示的位置,因此您需要完成一次图,然后获取补丁 2(橙色)上的流 2 和补丁 4(红色)上的流 1 的尖端位置.尖端位置的差异(x_adj,y_adj)是需要对补丁4的主干长度(以校正垂直方向)和补丁2上的流2的路径长度进行调整.

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

这篇关于使用 matplotlib 在桑基图中连接两个以上的系统使我错位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆