Colormap在NetworkX中不起作用 [英] Colormap not working in NetworkX
问题描述
我正在尝试制作彩色图表.我一直遇到奇怪的行为.
I am trying to make colored graphs. I keep running into odd behavior.
我无法获得颜色图来自动为节点分配颜色.我尝试用相同的颜色来完成所有节点!
I cannot get the colormap to assign colors automatically to the nodes. All nodes I try to do this with wind up the same color!
浮点数是应该分配给6个节点的颜色. 7个浮点中的两个是相同的,因为这是一个循环.
The floats are the colors that are supposed to be assigned to the 6 nodes. Two of the 7 floats are the same because it is a cycle.
当我手动指定节点的颜色(node_color=['r']
等)时,它不仅适用于根(红色),而且适用于循环中的节点,也可以正常工作.
When I manually specify the color of the nodes (node_color=['r']
, etc.) it works fine, not only for the root (in red), but for the nodes in the cycle as well.
代码:
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
是将名称映射到浮点的字典.为了完整起见,它们是:
Here, x
is an array of node names, and CD
is a dictionary mapping names to floats. For completeness, here they are:
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功能适用于我,因此我感觉自己犯了一个基本错误.有任何想法吗?
Colormap functionality works for me in other cases so I have the feeling I am making a basic error. Any ideas?
推荐答案
由于年代久远,拉克(Lack)在评论中回答了这个问题,因此我在此处复制他的回答并接受:
As this is old and Lack answered the question in the comments I am copying his answer here and accepting:
我无法正确运行您的代码(
TypeError: 'complex' object has no attribute '__getitem__' on j[t]
),但这与您回答的其他问题(stackoverflow.com/questions/27831022/…)是相同的问题.因为您一次只将一个节点传递给draw_networkx_nodes
,所以它标准化"了长度为1的颜色数组,而与其他节点无关.您应该摆脱循环,并将一个数组中的所有节点传递给draw_networkx_nodes
.
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 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
.
这篇关于Colormap在NetworkX中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!