颜色图在 NetworkX 中不起作用
Colormap not working in NetworkX
我正在尝试制作彩色图表。我保持 运行 行为古怪。
我无法让颜色图自动为节点分配颜色。我尝试用相同的颜色完成此操作的所有节点!
浮动是应该分配给 6 个节点的颜色。 7 个浮点数中有两个是相同的,因为它是一个循环。
当我手动指定节点的颜色(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
是一个映射名称到浮点数的字典。为了完整起见,它们是:
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}
Colormap 功能在其他情况下对我有用,所以我觉得我犯了一个基本错误。有任何想法吗?
因为这是旧的,而且 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 (whosebug.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
.
我正在尝试制作彩色图表。我保持 运行 行为古怪。
我无法让颜色图自动为节点分配颜色。我尝试用相同的颜色完成此操作的所有节点!
浮动是应该分配给 6 个节点的颜色。 7 个浮点数中有两个是相同的,因为它是一个循环。
当我手动指定节点的颜色(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
是一个映射名称到浮点数的字典。为了完整起见,它们是:
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}
Colormap 功能在其他情况下对我有用,所以我觉得我犯了一个基本错误。有任何想法吗?
因为这是旧的,而且 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 (whosebug.com/questions/27831022/…). Because you pass only one node at a time todraw_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 todraw_networkx_nodes
.