gpt4 book ai didi

python - 控制桑基图连接

转载 作者:太空狗 更新时间:2023-10-29 18:08:31 25 4
gpt4 key购买 nike

我正在尝试使用 Matplotlib 桑基图来控制哪些流相互连接。我正在修改基本的两个系统示例。

我认为我的困惑归结为误解了这实际上意味着什么:

Notice that only one connection is specified, but the systems form a circuit since: (1) the lengths of the paths are justified and (2) the orientation and ordering of the flows is mirrored.

我做了一个玩具示例,它使用单个数据集,然后为第二个系统修改它以确保所有数字都匹配。

import numpy as np
import matplotlib.pyplot as plt

from matplotlib.sankey import Sankey

plt.rcParams["figure.figsize"] = (15,10)


system_1 = [
{"label": "1st", "value": 2.00, "orientation": 0},
{"label": "2nd", "value": 0.15, "orientation": -1},
{"label": "3rd", "value": 0.60, "orientation": -1},
{"label": "4th", "value": -0.10, "orientation": -1},
{"label": "5th", "value": 0.25, "orientation": -1},
{"label": "6th", "value": 0.25, "orientation": -1},
{"label": "7th", "value": 0.25, "orientation": -1},
{"label": "8th", "value": 0.25, "orientation": -1},
{"label": "9th", "value": 0.25, "orientation": -1}
]

system_2 = system_1[:4]
system_2.append({"label": "new", "value": -0.25, "orientation": 1})


fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[], title="Where are all my cows?")
flows = [x["value"] for x in system_1]
labels = [x["label"] for x in system_1]
orientations=[x["orientation"] for x in system_1]
sankey = Sankey(ax=ax, unit="cow")
sankey.add(flows=flows,
labels=labels,
label='one',
orientations=orientations)

sankey.add(flows=[-x["value"] for x in system_2],
labels=[x["label"] for x in system_2],
label='two',
orientations=[-x["orientation"] for x in system_2],
prior=0,
connect= (0,0)
)

diagrams = sankey.finish()
diagrams[-1].patch.set_hatch('/')
plt.legend(loc='best')


plt.show()

这给了我:

A sankey diagram that doesn't really work

应该加入具有匹配标签的流。

我读过 thisthis但它们并没有帮助我理解实际发生的事情。

最佳答案

让我们从解决困惑开始

I think my confusion comes down to misunderstanding what this actually means:

Notice that only one connection is specified, but the systems form a circuit since: (1) the lengths of the paths are justified and (2) the orientation and ordering of the flows is mirrored.

(2) 流的方向和顺序是镜像的。

你可能理解错的是mirrored的意思,在这种情况下确实很困惑。有人会认为,镜像等于倒置,但这只是部分正确:
flows (或者正如您在代码中所说的那样:values)必须倒置,这个您做对了。因为values对应于输入(value > 0)或输出(value < 0)。并且只有输出可以连接到输入,反之亦然。

但是 orientation您尝试连接的两个流必须相同。这个不是倒置的,但它仍然需要“镜像”。这是什么意思?好吧,如果一个 I/O 正朝着他来的箭头方向看,它需要看到另一个 I/O(就像照镜子一样),只有这样它们才能连接自己。作为非母语人士,解释起来并不容易,但我会尝试说明这个想法:

Able to connect:         Not able to connect:        Not able to connect:
I/O Mirror I/O I/O Mirror I/O I/O Mirror I/O
╚══> | >══╝ ╗ | ╔ | ║
║ | ║ ══> | ║
v | ^ | ^

在您的代码中,您已经反转了 orientation .这就是为什么橙色系统的第三个流在左上角,而蓝色系统的第三个流在右下角。这些 I/O 永远无法“看到”彼此。

您可以通过删除 - 来恢复第二个系统的反转在 x 之前方向:

orientations=[x["orientation"] for x in system_2]

您会看到现在流彼此接近,但您处于 Not able to connect 中所示的情况。 -插图(第 2 号)。这意味着您的图表结构将无法以这种方式工作。您只能在这三个方向上弯曲单流:-90°、0° 或 90°。 orientations = -1, 0 or 1的通讯员.直接连接这些流的唯一方法是设置它们的 orientation=0 ,但在我看来,这不是您的目标。

您需要一种新的方法来完成这项任务,这样您就不会陷入像以前那样无法再连接流程的情况。我已经修改了您的代码以(也许?)达到您的目标。它看起来不再一样了,但我认为这是了解有关方向和镜像以及所有其他内容的概念的良好开端。

(1) 路径的长度是合理的。

您会在下面的代码中看到,我为 pathlengths 设置了值变量(在第二个系统中)。我的经验是,如果您有太多需要连接的流,matplotlib 将无法再自动完成。

代码和输出

import numpy as np
import matplotlib.pyplot as plt

from matplotlib.sankey import Sankey

plt.rcParams["figure.figsize"] = (15,10)


system_1 = [
{"label": "1st", "value": -2.00, "orientation": 1},
{"label": "4th", "value": 0.10, "orientation": 1},
{"label": "2nd", "value": 0.15, "orientation": 1},
{"label": "3rd", "value": 0.60, "orientation": 1},
{"label": "5th", "value": 0.25, "orientation": -1},
{"label": "6th", "value": 0.25, "orientation": -1},
{"label": "7th", "value": 0.25, "orientation": 1},
{"label": "8th", "value": 0.25, "orientation": 1},
{"label": "9th", "value": 0.25, "orientation": 0}
]

system_2 = [
{"label": "1st", "value": 2.00, "orientation": 1},
{"label": "4th", "value": -0.10, "orientation": 1},
{"label": "2nd", "value": -0.15, "orientation": 1},
{"label": "3rd", "value": -0.60, "orientation": 1},
{"label": "new", "value": -0.25, "orientation": 1}
]

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[], title="Where are all my cows?")

flows_1 = [x["value"] for x in system_1]
labels_1 = [x["label"] for x in system_1]
orientations_1=[x["orientation"] for x in system_1]

flows_2 = [x["value"] for x in system_2]
labels_2 = [x["label"] for x in system_2]
orientations_2=[x["orientation"] for x in system_2]

sankey = Sankey(ax=ax, unit=None)
sankey.add(flows=flows_1,
labels=labels_1,
label='one',
orientations=orientations_1)

sankey.add(flows=flows_2,
labels=labels_2,
label='two',
orientations=orientations_2,
pathlengths=[0, 0.4, 0.5, 0.65, 1.25],
prior=0,
connect=(0,0))

diagrams = sankey.finish()
diagrams[-1].patch.set_hatch('|')
diagrams[-0].patch.set_hatch('-')
plt.legend(loc='best')


plt.show()

Output

关于python - 控制桑基图连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49141083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com