Python 在matplotlib中连接两个Sankey图

Python 在matplotlib中连接两个Sankey图,python,matplotlib,sankey-diagram,Python,Matplotlib,Sankey Diagram,我试图用matplotlib来表示一个国家的天然气平衡 我的想法是,有三个进口天然气来源,我想使用一个Sankey绘制,并将其连接到另一个Sankey,该Sankey有其他天然气来源(生产、天然气储存)和天然气用户作为流出 我试了很多次,但我无法将两个图形连接在一起 每个图都按照设计分别绘制。但是,当我添加“prior=0,connect=(3,0)”时,所有的东西都会出错,给我带来一些我无法完全理解的错误。这是代码 import numpy as np import matplotlib.py

我试图用matplotlib来表示一个国家的天然气平衡

我的想法是,有三个进口天然气来源,我想使用一个Sankey绘制,并将其连接到另一个Sankey,该Sankey有其他天然气来源(生产、天然气储存)和天然气用户作为流出

我试了很多次,但我无法将两个图形连接在一起

每个图都按照设计分别绘制。但是,当我添加
“prior=0,connect=(3,0)”
时,所有的东西都会出错,给我带来一些我无法完全理解的错误。这是代码

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


ImportLabels=["Imports by\nNaftogaz","Imports by\nOstchem","Imports from\nEurope", ""]
ImportFlows=[11.493,9.768,1.935,-23.196]
ImportOrientation=[0,1,-1,0]

l=["Imports","Private\nextraction","State\nextraction","Net supplies\ntoUGS","Households","TKE","Metallurgy","Energy","Other\nIndustries","Technological\nlosses"]
v=[23.196,3.968,13.998,-2.252,-13.289,-7.163,-3.487,-4.72,-7.037,-3.17]
d=[0,-1,1,-1,0,0,1,1,1,-1]

sankey=Sankey(scale=1.0/69,patchlabel="Gas balance",format='%.1f',margin=0.15)
sankey.add(flows=ImportFlows, labels=ImportLabels, orientations=ImportOrientation, label='Imports',fc='#00AF00')
sankey.add(flows=v,labels=l,orientations=d, prior=0, connect=(3,0),label='Second',fc='#008000')
想法是将第一个图形的3个流出(其值为-23.196)与第二个sankey的0个流入(其值也为23.196)连接起来

下面是错误文本:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-24-1220fede42ce> in <module>()
     14 sankey=Sankey(scale=1.0/69,patchlabel="Gas balance",format='%.1f',margin=0.15)
     15 sankey.add(flows=ImportFlows, labels=ImportLabels,  orientations=ImportOrientation, label='Imports',fc='#00AF00')
---> 16 sankey.add(flows=v,labels=l,orientations=d, prior=0, connect=(3,0),label='Second',fc='#008000')

C:\Python27\lib\site-packages\matplotlib\sankey.pyc in add(self, patchlabel, flows, orientations, labels, trunklength, pathlengths, prior, connect, rotation, **kwargs)
    369                    ("The connection index to the source diagram is %d, but "
    370                     "that diagram has only %d flows.\nThe index is zero-based."
--> 371                     % connect[0], len(self.diagrams[prior].flows))
    372             assert connect[1] < n, ("The connection index to this diagram is "
    373                                     "%d, but this diagram has only %d flows.\n"

TypeError: not enough arguments for format string
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
14 sankey=sankey(比例=1.0/69,patchlabel=“气体平衡”,格式='%.1f',裕度=0.15)
15 sankey.add(流=importflow,标签=importlabel,方向=ImportOrientation,标签='Imports',fc='#00AF00')
--->16 sankey.add(流量=v,标签=l,方向=d,优先级=0,连接=(3,0),标签=Second',fc='#008000')
C:\Python27\lib\site packages\matplotlib\sankey.pyc in add(self、patchlabel、流、方向、标签、中继长度、路径长度、优先级、连接、旋转、**kwargs)
369(“到源关系图的连接索引是%d,但是”
370“该图只有%d个流。\n索引是从零开始的。”
-->371%连接[0],len(self.diagrams[preor].flows))
372断言连接[1]

因此,我不确定两个图形之间的连接是否有问题(由sankey.pyc试图显示的文本建议),或者matplotlib本身是否有问题,因为
“TypeError:格式字符串的参数不足”
表示?

您的问题是放置了两个
.add()
调用。对
Sankey()
的第一个调用已经构建了一个图表(默认为灰色,有1个流入和1个流出)。因此,当您尝试连接到第一个图表时,它失败了,因为它只有一个流,而您正试图连接到第三个流。(无论如何,它都会失败,因为流不匹配。)

您需要在第一次调用中设置第一个图表,并且只有一个添加调用,例如:

sankey = Sankey(scale=1.0/69,patchlabel="Gas balance",format='%.1f',margin=0.15,
                flows=ImportFlows, labels=ImportLabels, 
                orientations=ImportOrientation, label='Imports',fc='#00AF00')
sankey.add(flows=v,labels=l,orientations=d, label='Second',fc='#008000', prior=0,
           connect=(3, 0))
这给了我:

对于较新的版本,我认为正确的错误消息是断言错误:源图的连接索引是3,但该图只有2个流。索引是零基的。
谢谢,蒂亚戈!!这很有趣,因为该图有4个流。3个流入和一个流出。谢谢你让我知道我的版本不是他是最新的。我对Python非常陌生,虽然easy_install会自动安装最新版本。显然不是。太好了!谢谢!我用不同的方法解决了我的问题…我删除了patchlabel=“Gas balance”,它成功了。可能是一些奇怪的错误!你的版本甚至可以在不删除补丁标签的情况下运行。