I'm trying to add new nodes (red dots) with new edges (green lines) to be places diagonaly new color and positions to this grid graph
import networkx as nx
import matplotlib.pyplot as plt
G = nx.grid_graph(dim=[5, 5])
nodes = list(G.nodes)
edges = list(G.edges)
p = []
for i in range(0, 5):
for j in range(0, 5):
p.append([i, j])
for i in range(0, len(nodes)):
G.nodes[nodes[i]]['pos'] = p[i]
pos = {}
for i in range(0, len(nodes)):
pos[nodes[i]] = p[i]
nx.draw(G, pos)
plt.show()
Sorry, your question is not clear for me, but you can create list of edges and then use G.add_edges_from()
to add nodes & edges to your initial graph
Probably you should define algorithm to find coordinates / labels of new nodes, then construct edges and add by G.add_edges_from()
G = nx.grid_graph(dim=[5, 5])
new_edges = [((0.5, 1.5), (0.5, 2.5)),
((0.5, 1.5), (0, 2)),
((0.5, 2.5), (0, 2))]
G.add_edges_from(new_edges)
nx.draw(G, pos={n:n for n in G.nodes()})
plt.show()
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.