python - 使用 matplotlib 在 sankey 图中连接两个以上系统会使我错位
问题描述
我正在尝试使用 matplotlib 制作一个稍微复杂的 sankey 图,它应该是动态的,因为我应该能够更改流的值并且所有连接都应该保持连接。这意味着我不能像本例中建议的那样手动调整路径长度。为了让自己熟悉显式和隐式连接,我尝试从这个仅通过一个显式连接连接在一起的两个系统的示例中继续构建。使用此示例,您可以更改流的值,并使事物保持正确连接。所以我尝试添加从系统 1 到系统 2 的第四个系统,但我似乎无法让隐式连接正常工作。请参阅下面的代码和输出。
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"
任何有关制作具有多个连接系统的动态桑基图的提示或技巧将不胜感激。
解决方案
我的想法是,在图表完成之前,您无法知道各个提示的位置,因此您需要完成图表一次,然后在补丁 2(橙色)和流程 1 上获取流程 2 的提示位置在补丁 4(红色)上。尖端位置 (x_adj,y_adj) 的差异是需要对补丁 4 的树干长度(以校正垂直方向)和补丁 2 上的流 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()
推荐阅读
- java - 使用 Spring Boot 和 Eureka Server 时如何修复错误 java.net.UnknownHostException?
- javascript - 如何声明“承诺”构造函数?
- python - 如何将 13 位 unix 纪元时间格式转换为日期时间?
- authentication - 在 Tomcat 中使用基于表单的身份验证的 Ember Web 应用程序
- laravel - 如何使用 Chart.js 在 Laravel 中传递数据
- html - 使用 BeautifulSoup 从网页下载损益表并转换为 Pandas 数据框?
- excel - 如何输入数组中行数的值?
- apache - 在 htaccess 中重写 url
- java - Prevent uploading additional "app-unspecified.aar" to Bintray
- python - Python 请求发送带有数据的图像