gpt4 book ai didi

Python Plotly 桑基图 : Handling circular references

转载 作者:行者123 更新时间:2023-12-05 07:29:00 25 4
gpt4 key购买 nike

我正在尝试使用 plotly 在桑基图中表示大量数据。问题是我的数据有周期并且它们不受 plotly 的支持。所以我试图正确地删除它们:我想找到这些循环并根据它们的值将它们之间的差异保留在 Sankey 中。

我们拿这篇帖子的代码来说:Python Plotly Sankey Graph not showing up

import plotly.offline

data_trace = {'domain': {'x': [0, 1], 'y': [0, 1]},
'height': 772,
'link': {'label': ['EM', 'GWF9C51E', 'GWF9C511', 'GWF9C51E Sensor Set',
'GWF9C511 Sensor Set'],
'source': [0, 1, 3, 1, 4, 2, 0, 2],
'target': [1, 3, 1, 0, 2, 0, 2, 4],
'value': [40, 76, 29, 86, 30, 75, 41, 65]},
'node': {'color': ['blue', 'yellow', 'yellow', 'green', 'green'],
'label': ['EM', 'GWF9C51E', 'GWF9C511', 'GWF9C51E Sensor Set',
'GWF9C511 Sensor Set'],
'line': {'color': 'black', 'width': 0.5},
'pad': 15,
'thickness': 15},
'orientation': 'h',
'type': 'sankey',
'valueformat': '.3s',
'valuesuffix': 'pkts',
'width': 1118}
layout = dict(
title = "Testing Sankey",
font = dict(
size = 10
)

fig = dict(data=[data_trace], layout=layout)
plotly.offline.plot(fig, validate=False)

我正在努力获得以下输出:

source = [1, 1, 2, 2]
target = [3, 0, 0, 4]
value = [47, 46, 34, 35]

最后,我在我的 sankey 类中添加了一个方法:

def preprocess_data(self, source, target, value):
equal = True
while equal:
equal = False
for i in range(len(source)):
if source[i] == target[i]:
source.remove(source[i])
target.remove(target[i])
value.remove(value[i])
equal = True
break
cycles = True
while cycles:
cycles = False
for i in range(len(source)):
if source[i] in target:
if source[target.index(source[i])] == target[i]:
if value[i] > value[target.index(source[i])]:
value[i] = value[i] - value[target.index(source[i])]
source.remove(source[target.index(source[i])])
value.remove(value[target.index(source[i])])
target.remove(target[target.index(source[i])])
cycles = True
break
elif value[i] < value[target.index(source[i])]:
value[target.index(source[i])] = value[target.index(source[i])] - value[i]
source.remove(source[i])
target.remove(target[i])
value.remove(value[i])
cycles = True
break
elif value[i] == value[target.index(source[i])]:
source.remove(source[target.index(source[i])])
value.remove(value[target.index(source[i])])
target.remove(target[target.index(source[i])])
source.remove(source[i])
target.remove(target[i])
value.remove(value[i])
cycles = True
break
return source, target, value

但我仍然遇到错误:Sankey 数据中存在循环。删除所有节点和链接。这是我的数据:

Source  Target  Value
1 0 3.055036682
2 1 0.255226984
3 1 0.17286605
4 1 0.283457885
5 1 2.189129762
6 1 0.146398649
7 1 0.000330826
8 5 0.014816468
9 5 1.761388323
10 5 0.007640889
11 5 0.034437287
12 5 0.36981087
13 9 0.051029227
14 9 0.048415849
15 9 0.025553877
16 9 0.014874949
17 9 0.026363222
18 9 0.027792755
19 9 0.03991485
20 9 1.473528785
21 9 0.05176817
8 20 0.013481225
10 20 0.010946264
22 20 1.175625342
23 20 0.51347902
24 20 0.043903806
11 20 0.024993631
25 22 0.75991767
26 22 0.145394536
27 22 0.005930888
28 22 0.012234177
29 22 0.002055835
30 22 0.018474162
31 22 0.156176541
32 22 0.033256631
33 22 0.003159624
34 22 0.018618654
35 22 0.010317747
36 25 0.727858859
37 25 0.039782352
27 36 0.00945708
38 36 0.000444879
39 36 0.040179593
40 36 0.677729291
41 40 0.01814106
42 40 0.153769306
43 40 0.327314909
8 40 0.000454993
44 40 0.060977151
45 40 0.066703595
46 40 0.029803695
11 40 0.002020937
47 40 0.332885758
31 23 0.083713487
15 23 0.013335976
16 23 0.046455924
48 23 0.025814488
19 23 0.011392122
21 23 0.332460405
10 21 0.006533316
49 21 0.343844244
50 21 0.029481036
51 21 0.012668768
25 12 0.012360508
26 12 0.005031381
13 12 0.010749498
30 12 0.003701076
17 12 0.005553512
34 12 0.006710585
20 12 0.310404369
39 12 0.009207691
52 49 0.000207941
14 49 0.020680254
29 49 0.000317917
32 49 0.005142853
33 49 0.000488609
40 49 0.314920228
53 49 0.000366989
35 49 0.001595551
29 47 2.38345E-05
32 47 0.000385564
33 47 3.66314E-05
6 47 3.64378E-06
54 47 4.81106E-06
55 47 0.154840432
56 47 6.63752E-06
57 47 0.001367098
53 47 2.75135E-05
58 47 8.17783E-06
59 47 5.75289E-06
29 43 7.77673E-05
60 43 5.05489E-07
32 43 0.001258018
17 43 2.37004E-05
33 43 0.000119521
61 43 0.30422448
21 43 4.66976E-07
53 43 8.97709E-05
62 43 0.014012209
35 43 0.000390296
63 61 0.300086221
64 61 0.041554598
65 61 0.032388115
8 61 9.40855E-06
66 61 0.324759598
67 61 0.147241861
11 61 6.93695E-05
68 61 0.157452076
69 61 0.017542372
57 66 0.009835633

关于为什么我仍然收到该错误的任何线索?谢谢!

最佳答案

我认为 plotly sankey 确实支持圆图(现在)?

我有时为去除复杂循环所做的事情是使用像网络 X 这样的图形库进行转换,它有一些 API 方法来去除自循环 Removing self-loops from undirected networkx graph

关于Python Plotly 桑基图 : Handling circular references,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53042938/

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