Colormap does not work in NetworkX

I am trying to make color graphics. I continue with strange behavior.

I can't seem to get a color picker to automatically assign colors to nodes. All the nodes I am trying to make are with the same color!

enter image description here

Floats are the colors to be assigned to 6 nodes. Two of the 7 floats are the same because it is a loop.

When I manually specify the color of the nodes ( node_color=['r']

etc.), it works great not only for the root (in red), but also for the nodes in the loop.

Code:

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')         

      

Here x

is an array of node names, and CD

are dictionary mappings names for 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}

      

The Colormap function works for me in other cases, so I feel like I'm making a basic mistake. Any ideas?

+1


source to share


1 answer


Since this is old and Lack answered the question in the comments, I copy his answer here and accepting:



I cannot run your code without errors ( TypeError: 'complex' object has no attribute '__getitem__' on j[t]

), but this is the same problem as your other question I answered (stackoverflow.com/questions/27831022 / ...). Since you are only passing one node at a time before draw_networkx_nodes

, it will "normalize" a 1-length array of colors without considering any other nodes. You have to get rid of the loop and pass all the nodes in one array to draw_networkx_nodes

.

+1


source







All Articles