简体   繁体   中英

Plotting the same graph repeatedly — how to get nodes in the same position?

I am working in Python, using NetworkX and Matplotlib.

When I plot the same graph over and over but with different colors, how can I get the nodes to take the same position every time? Right now I am getting:

在此处输入图片说明 But I am adding the nodes as keys of a dictionary, and the color of each node as the value, and then sorting the dictionary and passing the nodes as the keys of the sorted dictionary and the colors as the values of the sorted dict. The same nodes are always added in the same order. I thought that would work...

So, where x holds lists of nodes (branches):

for ct2,i in enumerate(x):
        for ct,j in enumerate(i):
            vertex =  j[t] 

            if np.angle(j[t]) <0 or np.angle(j[t]) >= np.angle(cutoff):
                C[vertex] = 0.0
            else:
                C[vertex] = .8- 3*(np.angle(j[t])/np.angle(cutoff))
    COLORS =  collections.OrderedDict(sorted(C.items())) 

Then the graphing call:

pos=nx.graphviz_layout(G,'dot')

nx.draw_networkx_nodes(
    G,pos,nodelist=COLORS.keys(),cmap=plt.get_cmap(cmap),
    node_size=nodesize,alpha=.6,vmax=1,vmin=0, node_color = COLORS.values() 
)

What am I doing wrong?

It would be good to see where the graphing call sits relative to your loop (inside? outside?)

But it looks like you've got pos=nx.graphviz_layout(G,'dot') within the loop. So each time within the loop it recalculates pos . This is the variable that tells the algorithm where to put the nodes. The position is somewhat random, so each call puts them in a different place (this is more obvious with spring_layout).

If this is what you've done, just move

pos=nx.graphviz_layout(G,'dot')

before the loop. Then it won't be regenerated each time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM