简体   繁体   English

重复绘制同一图-如何使节点处于相同位置?

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

I am working in Python, using NetworkX and Matplotlib. 我正在使用NetworkX和Matplotlib在Python中工作。

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. 但是我将节点添加为字典的键,并将每个节点的颜色作为值,然后对字典进行排序,并将节点作为已排序的字典的键,并将颜色作为已排序dict的值。 The same nodes are always added in the same order. 相同的节点始终以相同的顺序添加。 I thought that would work... 我以为那行得通...

So, where x holds lists of nodes (branches): 因此,其中x包含节点(分支)列表:

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. 但是看起来循环中有pos=nx.graphviz_layout(G,'dot') So each time within the loop it recalculates pos . 因此,每次在循环中,它都会重新计算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). 该位置有些随机,因此每次调用都会将它们放置在不同的位置(对于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. 这样就不会每次都重新生成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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