我尝试使用下面的matplotlib代码绘制一个Sankey图。当我尝试分别绘制它们时,代码工作正常。但是,当我合并它们时,其中一个箭头的方向在组合时是相反的。我试图排除故障,但我完全忘记了为什么会发生这种情况。有人能提供反馈吗?
a = [1008590.0368940466, -368142.35465001286, -34409.668598874494, -369212.57399895444, -236736.40069105377, -89.03895515092165]
b = [199910.4894048177, 15518.603443203947, 143514.46418879204, 231094.9357130916, 44.28348931634303, -590082.7762392217]
from matplotlib.sankey import Sankey
from matplotlib import pyplot as plt
fig = plt.figure(figsize = [10,5], dpi = 300)
ax = fig.add_subplot(1, 1, 1,)
sankey = Sankey(ax=ax,
scale=0.0000003,
offset= 0,
format = '%d',shoulder = 0.02)
sankey.add(flows=a,
labels = ['Input', 'Low', 'Medium', 'High', 'Remote (Forest)', 'Other'],
orientations=[0, -1, -1, -1, 0, 1],#arrow directions,
pathlengths = [0.05,0.1,0.3,0.2,0.1, 0.1],
trunklength = 0.5,
edgecolor = '#2b8cbe',
facecolor = '#2b8cbe')
diagrams = sankey.finish()
for diagram in diagrams:
for text in diagram.texts:
text.set_fontsize(10);
plt.axis("off")
plt.show()
fig = plt.figure(figsize = [10,5], dpi = 300)
ax = fig.add_subplot(1, 1, 1,)
sankey = Sankey(ax=ax,
scale=0.0000006,
offset= 0,
format = '%d',shoulder = 0.02)
sankey.add(flows=b,
labels = ['Low', 'Medium', 'High', 'Remote (Forest)-I', 'Other', 'Forest-Resilience'],
orientations=[-1, -1, -1, 0, 1, 0],#arrow directions,
pathlengths = [0.2,0.42,0.6,0.1,0.1,0.1],
trunklength = 0.5,
connect = (4,3),
edgecolor = '#2ca25f',
facecolor = '#2ca25f')
diagrams = sankey.finish()
for diagram in diagrams:
for text in diagram.texts:
text.set_fontsize(10);
plt.axis("off")
plt.show()
组合地块代码:
from matplotlib.sankey import Sankey
from matplotlib import pyplot as plt
fig = plt.figure(figsize = [10,5], dpi = 330)
ax = fig.add_subplot(1, 1, 1,)
sankey = Sankey(ax=ax,
scale=0.0000006,
offset= 0,
format = '%d',shoulder = 0.02, tolerance=6000)
## Had to add tolerance, otherwise the plots would not combine
sankey.add(flows=a,
labels = ['Input', 'Low', 'Medium', 'High', 'Remote (Forest)', 'Other'],
orientations=[0, -1, -1, -1, 0, 1],#arrow directions,
pathlengths = [0.05,0.1,0.3,0.2,0.1, 0.1],
trunklength = 0.5,
edgecolor = '#2b8cbe',
facecolor = '#2b8cbe')
sankey.add(flows=b,
#labels = ['Low', 'Medium', 'High', 'Remote (Forest)-I', 'Other', 'Forest-Resilience'],
orientations=[-1, -1, -1, 0, 1, 0],#arrow directions,
pathlengths = [0.2,0.42,0.6,0.1,0.1,0.1],
trunklength = 0.5,
prior =0, connect = (4,3),
edgecolor = '#2ca25f',
facecolor = '#2ca25f')
sankey.add(flows=[b[0],-b[0]],
#labels = ['Low'],
orientations=[1, 1],#arrow directions,
pathlengths = [0.1,0.1],
trunklength = 0.27,
prior = 1, connect = (0,1),
edgecolor = None,
facecolor = '#99d8c9', hatch = '....', zorder = -2, label = 'False', alpha = 0.2)
diagrams = sankey.finish()
diagrams[0].texts[4].set_position(xy=[1.1, 0.4])
diagrams[1].texts[3].set_position(xy=[1.55, 0.23])
diagrams[2].texts[0].set_position(xy=[1.55,-0.2])
for diagram in diagrams:
for text in diagram.texts:
text.set_fontsize(10);
plt.axis("off")
plt.show()
检查 '44'(绿色) 箭头的方向。在上图和下图中方向相反)。
1条答案
按热度按时间b5buobof1#
44小于公差。这个流量的大小是44±6000,所以基本上你告诉它,你不知道这是一个输入还是一个输出,导致你看到的行为。
您可以将该容差设置为连接流
Remote (Forest)
(236736)和Remote (Forest)-I
(231094)之间差异的解决方法。我建议为缺失的I
添加一个流,这样就不需要依赖tolerance来捏造数字。