【问题标题】:Connecting two Sankey diagrams in matplotlib在 matplotlib 中连接两个 Sankey 图
【发布时间】:2013-12-23 21:00:33
【问题描述】:

我正在尝试使用 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

所以我不确定是两个图之间的连接有问题(由 sankey.pyc 试图显示的文本建议)还是 matplotlib 本身有问题,如"TypeError: not enough arguments for format string" 所示?

【问题讨论】:

  • 对于更新的版本,我正确的错误消息是AssertionError: The connection index to the source diagram is 3, but that diagram has only 2 flows. The index is zero-based.
  • 谢谢蒂亚戈!!这很有趣,因为该图有 4 个流程。 3 流入和 1 流出。感谢您让我知道我的版本不是最新的。我对 Python 很陌生,尽管 easy_install 会自动安装最新版本。显然不是。

标签: python matplotlib sankey-diagram


【解决方案1】:

您的问题是您要拨打两个 .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))

这给了我:

【讨论】:

  • 太棒了!谢谢!我以不同的方式解决了我的问题......我删除了 patchlabel="Gas balance" 并且它起作用了。可能是一些奇怪的错误!即使不删除补丁标签,您的版本也能正常工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-04
  • 1970-01-01
相关资源
最近更新 更多