简体   繁体   English

如何将带有(新节点,新边)的子图添加到 python 中的现有图

[英]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.

相关问题 在networkx python中,如何将一组新的节点和边连接到图中的每个节点? - In networkx python, how to connect a new bunch of nodes and edges to each node in a graph? 如何将 a.networkx 多部分图的 plot 子图作为具有刷新位置的新图 - How to plot subgraph of a networkx multipartite graph as a new graph with refreshed positions 如何在 Python 中构建图(节点和边)? - How to construct a graph (nodes and edges) in Python? 通过networkx并使用python在图中添加节点和边 - Add nodes & edges in graph via networkx and using python python igraph 边作为节点图 - python igraph edges as nodes graph 如何根据 python 中两个现有字典键值的条件匹配向字典添加新键? - How to add a new key to a dictionary based on condition match of two existed dict key values in python? 如何用字典将新字典添加到已存在的json文件中? - How to add new dictionary into existed json file with dictionary? 如何在Networkx中向网络分析图添加节点和边? - How to add nodes and edges to a network analysis graph in Networkx? Python:如何从csv文件创建图形节点和边? - Python: How to create graph nodes and edges from csv file? 如何使用 python 中的节点和边在图形中绘制形状? - How to draw shapes in a graph using nodes and edges in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM