python - 在 Networkx 中映射两个图

标签 python graph networkx

这些天我正在努力根据两个不同的图形的共同节点来映射它们。这是两个图表的样子。节点的颜色表示蓝色节点为优先级1黄色节点为优先级2

共享同一个节点的节点,我想将它们组合起来。这就是我想要实现的目标。在这种情况下,10和190分别是31、130、204和240的公共(public)节点。

这是我尝试过的代码。

for node1, node2 in Graph2.edges():
    if Graph1.has_edge(node1, node2):
        for attr in Graph2.adj[node1][node2]:
            if (Graph1.node[node1]['priority']==1):

                name = str(node1) + '/' + str(node2)            
                mapping = {n1: name, node2: name}
                Graph2 =nx.relabel_nodes(Graph2, mapping)

    pos1 = nx.shell_layout(Graph2) 

    nodes = nx.draw_networkx_nodes(Graph2, pos1, node_size=2000, node_color = 'b' , alpha=0.3, with_labels = True) 
    nx.draw_networkx_edges(Graph2, pos1, width=1.5, alpha=0.3, edge_color='k')
    nx.draw_networkx_labels(Graph2, pos1, font_size=15, font_family='sans-serif')  

有人可以帮我实现这个目标吗?

非常感谢!!

最佳答案

对于那些正在寻找类似解决方案的人,我尝试解决优先级 1 的上述问题,这意味着蓝色节点。对于其余的优先事项也执行同样的操作。这里的优先级和名称是我的图形属性。

    try:
        for n1, n2, attr in list(graph1.edges(data ='True')):
            for x1, x2, attr1 in list(graph2.edges(data ='True')):
                if ((graph1.node[n1]['priority'] ==1) & (graph1.node[n1]['name'] =='Start')):
                    if ((graph2.node[x1]['priority'] ==1) & (graph2.node[x1]['name'] =='Start')):

                        name1 = str(n1) + '/' + str(x1)
                        mapping1 = {n1: name1, x1:name1}
                        graph1 =nx.relabel_nodes(graph1, mapping1)
                        graph2 =nx.relabel_nodes(graph2, mapping1)


    except KeyError:
        print('')   

    try:
        for n1, n2, attr in list(graph1.edges(data ='True')):
            for x1, x2, attr1 in list(graph2.edges(data ='True')):
                if ((graph1.node[n2]['priority'] ==1) & (graph1.node[n2]['name'] =='End')):
                    if ((graph2.node[x2]['priority'] ==1) & (graph2.node[x2]['name'] =='End')):

                        name1 = str(n2) + '/' + str(x2)
                        mapping1 = {n2: name1, x2:name1}
                        graph1 =nx.relabel_nodes(graph1, mapping1)
                        graph2 =nx.relabel_nodes(graph2, mapping1)


    except KeyError: 
        print('')  

关于python - 在 Networkx 中映射两个图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58534054/

相关文章:

python - 在 python 中绘制自定义图表

Python Networkx 桥接检测

python - 在 networkx 中从节点名称映射到它的索引,反之亦然

python - 使用 python 透视 CSV 字符串,而不使用 pandas 或任何类似的库

Python 创建用户输入表单并将输入转换为数据帧

algorithm - 寻找算法:最小切割以生成二部图

graph - 为什么广度优先搜索中除了黑色和白色以外,还需要给一个节点涂上灰色呢?

Python peewee.ImproperlyConfigured : MySQL driver not installed

python - GitPython pull/fetch 检索进度

r - 一张图上有多条线 : Adding a text acronym ("legend") to the very the end of each line (ggplot2)