[英]how to add subgraph with (new nodes, new edges) to an existed graph in python
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抱歉,我不清楚您的问题,但您可以创建边列表,然后使用G.add_edges_from()
将节点和边添加到初始图中
Probably you should define algorithm to find coordinates / labels of new nodes, then construct edges and add by G.add_edges_from()
可能您应该定义算法来查找新节点的坐标/标签,然后构造边并通过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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.