[英]NetworkX: plotting a graph in a for loop returns the wrong graph
I am working with a regular network (grid) that I create as:我正在使用我创建的常规网络(网格):
import networkx as nx
N=100
def graph_creating(N):
G=nx.grid_2d_graph(N,N)
pos = dict( (n, n) for n in G.nodes() ) #Dictionary of all positions
return G, pos
I have two ways of iterating the code.我有两种迭代代码的方法。 In both I remove nodes from the network and attempt to draw it.在两者中,我从网络中删除节点并尝试绘制它。 Depending on whether I create the network initially or within the loop, I get a different behavior when I plot it.根据我是最初创建网络还是在循环内创建网络,我在绘制它时会得到不同的行为。
My problem: I want to plot the grid after stage 1 and after stage 2 , so to make comparisons with the unaltered grid/graph.我的问题:我想在 stage 1和stage 2之后绘制网格,以便与未更改的网格/图形进行比较。 I fail to do it properly because:我没有正确地做到这一点,因为:
for
loop, I correctly get the first plots but later plots are empty as the graph is never restored back to its unaltered statu;如果原始网格是在for
循环之外创建的,我会正确地得到第一个图,但后来的图是空的,因为图永远不会恢复到其未更改的状态;for
loop, I always end up having the unaltered grid plotted, as if the removal had no effect on it.如果原始网格是在for
循环内创建的,我总是最终绘制未更改的网格,就好像删除对其没有影响一样。Where else should the graph-creating block be placed, in order to be able to plot the graph right after stage 1 and 2?为了能够在第 1 阶段和第 2 阶段之后立即绘制图形,还应该将图形创建块放置在哪里?
version 1 the graph is created outside the for
loop:版本 1图形是在for
循环之外创建的:
G, pos = graph_creating(N)
nodelist = G.nodes()
for counter in range(5):
G1 = nodelist[2*counter:2*counter+1]
G.remove_nodes_from(G1)
nx.draw_networkx(G, pos = pos)
figurename = 'file{0}.png'.format(counter)
plt.savefig(figurename)
G2=nodelist[2*counter+1:2*counter+2]
G.remove_nodes_from(G2)
nx.draw_networkx(G,pos=pos)
#it's not clear from your original question if you save this figure or not
Result: only the first iteration produces correct plots.结果:只有第一次迭代产生正确的图。 Later plots are empty as the graph is never restored back to its unaltered status.后面的图是空的,因为图永远不会恢复到其未更改的状态。
version 2 the graph is created inside the for
loop:版本 2图形是在for
循环内创建的:
for counter in range(5):
G, pos = graph_creating(N)
nodelist = G.nodes()
G1 = nodelist[2*counter:2*counter+1]
G.remove_nodes_from(G1)
nx.draw_networkx(G, pos = pos)
figurename = 'file{0}.png'.format(counter)
plt.savefig(figurename)
G2=nodelist[2*counter+1:2*counter+2]
G.remove_nodes_from(G2)
nx.draw_networkx(G,pos=pos)
#it's not clear from your original question if you save this figure or not
Result: the calls to nx.draw_networkx
result in the unaltered graph being plotted for each iteration.结果:对nx.draw_networkx
的调用导致每次迭代都绘制未nx.draw_networkx
的图形。 I wonder if the problem is in the way I call that function, as it always plots the graph with no failed nodes.我想知道问题是否出在我调用该函数的方式上,因为它总是绘制没有失败节点的图形。 Why do I have this plotting problem?为什么我有这个绘图问题? . .
tmp_G = G.copy()
在 for 循环内使用tmp_G = G.copy()
创建图形的副本draw_netwokx
or remove_nodes_from
use tmp_G instead of G使用draw_netwokx
或remove_nodes_from
使用tmp_G而不是G
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.