I am generating random Geometric graph using networkx. I am exporting all the node and edges information into file.
I want to generate the same graph by importing all the node and edges information from file.
Code to export the node values and edge information.
G=nx.random_geometric_graph(10,0.5)
filename = "ipRandomGrid.txt"
fh=open(filename,'wb')
nx.write_adjlist(G, fh)
nx.draw(G)
plt.show()
I am trying to export it with below code and trying to change the color of some nodes. But it is generating different graph.
filename = "ipRandomGrid.txt"
fh=open(filename, 'rb')
G=nx.Graph()
G=nx.read_adjlist("ipRandomGrid.txt")
pos=nx.random_layout(G)
nx.draw_networkx_nodes(G,pos,nodelist=['1','2'],node_color='b')
nx.draw(G)
plt.show()
How to generate the same graph with few changes in color of some nodes?
If I understand the problem you’re having correctly, the trouble is here:
You create a random layout of the graph in the first line, and use it to draw nodes
'1'and'2'in the second line. You then draw the graph again in the third line without specifying the positions, which uses a spring model to position the nodes.Your graph has no extra nodes, you’ve just drawn two of them in two different positions. If you want to consistently draw a graph the same way, you need to consistently use the
posyou calculated. If you want it to be the same after storing and reloading, then saveposas well.