gpt4 book ai didi

python - 颜色图在 NetworkX 中不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 17:59:56 26 4
gpt4 key购买 nike

我正在尝试制作彩色图表。我不断遇到奇怪的行为。

我无法获取颜色图来自动为节点分配颜色。我尝试这样做的所有节点都以相同的颜色结束!

enter image description here

float 是应该分配给 6 个节点的颜色。 7 个 float 中有两个相同,因为它是一个循环。

当我手动指定节点的颜色(node_color=['r']等)时,它工作得很好,不仅对于根(红色),而且对于循环也是如此。

代码:

t=0
import networkx as nx
import matplotlib.pyplot as plt
G = nx.DiGraph()

#MAKE
G.add_node("ROOT")
#make all others
for i in x:
for ct,j in enumerate(i):
G.add_node(j[t] )
if ct ==0:
G.add_edge("ROOT", j[t])
else:
G.add_edge(i[ct-1][t], i[ct][t])
nx.write_dot(G,'g')
#DRAW
pos=nx.graphviz_layout(G,prog='neato')
nx.draw_networkx_nodes(G,pos, nodelist=['ROOT'], node_color=['r'])
#draw all others
for i in x:
for ct,j in enumerate(i):
print CD[j[t]]#, np.around([CD[j[t]]],decimals=2)
nx.draw_networkx_nodes(G,pos, nodelist = [j[t]], cmap=plt.get_cmap('Set3') ,node_color=np.around([CD[j[t]]],decimals=2))#float(c) for c in nodecolors(x[i],1)] )
nx.draw_networkx_edges(G, pos,arrows=True)

#Display properties
limits=plt.axis('off')

这里,x 是一个节点名称数组,CD 是将名称映射到 float 的字典。为了完整起见,它们如下:

x = [[(1.000004+0j)], [(-0.5000065+0.86602454j)], [(-0.5000065-0.86602454j)],[(1.000004+0j)],[(-0.5000065+0.86602454j)],[(-0.5000065-0.86602454j)]]

CD = {(-0.50000649677999998-0.8660245358880001j): 0.7142857142857143,
(-0.50000649677999998+0.8660245358880001j): 0.5714285714285714,
(-0.50000049676800007-0.86603492822400008j): 0.14285714285714285,
(-0.50000049676800007+0.86603492822400008j): 0.42857142857142855,
0j: 0.0,
(0.99999200001600019-0j): 0.8571428571428571,
(1.000004000004+0j): 0.2857142857142857}

颜色图功能在其他情况下适用于我,所以我感觉我犯了一个基本错误。有任何想法吗?

最佳答案

由于这是旧的,并且 Lack 在评论中回答了问题,我在这里复制他的答案并接受:

I can't run your code without errors (TypeError: 'complex' object has no attribute '__getitem__' on j[t]) but it's the same problem as your other question which I answered (stackoverflow.com/questions/27831022/…). Because you pass only one node at a time to draw_networkx_nodes, it "normalizes" the length-1 array of colors without regard to the other nodes. You should get rid of the loop and pass all the nodes in one array to draw_networkx_nodes.

关于python - 颜色图在 NetworkX 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27871827/

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