【问题标题】:networkx: draw network as separating two types of nodesnetworkx:将网络绘制为分隔两种类型的节点
【发布时间】:2017-02-15 02:21:41
【问题描述】:

我正在尝试用 python 的 networkx 绘制一个网络。

我有两种类型的节点,这些类型的节点应该分开放置。如何分别放置不同类型的节点?

例如,请看下面的节点。

我希望将红色节点(狗、牛、猫)与蓝色节点(汽车、笔、纸、杯子)分开,如下图所示。

那么,我的问题是networkx 是如何绘制出像上图那样分隔节点组的这种网络

作为参考,我粘贴了绘制第一张图片的代码。

import networkx as nx
import matplotlib.pyplot as plt

G = nx.Graph()
target_word_list = ["dog", "cow", "cat"] # represented by red nodes
attribute_word_list = ["car", "pen","paper", "cup"] # represented by blue nodes
word_list = target_word_list + attribute_word_list

for i in range(0, len(word_list)):
    G.add_node(i)

pos=nx.spring_layout(G) # positions for all nodes
# draw nodes
nx.draw_networkx_nodes(G,pos,
                       nodelist=range(0, len(target_word_list)),
                       node_color='r',
                       node_size=50, alpha=0.8)
nx.draw_networkx_nodes(G,pos,
                       nodelist=range(len(target_word_list), len(word_list)),
                       node_color='b',
                       node_size=50, alpha=0.8)    
labels = {}
for idx, target_word in enumerate(target_word_list):
    labels[idx] = target_word
for idx, attribute_word in enumerate(attribute_word_list):
    labels[len(target_word_list)+idx] = attribute_word
nx.draw_networkx_labels(G,pos,labels,font_size=14)

plt.axis('off')

【问题讨论】:

    标签: python networkx


    【解决方案1】:

    您可以手动上下移动一组节点的 y 坐标。 所以,如果你的节点坐标在pos:

    for i in range(0, len(word_list)):
        if word_list[i] in attribute_word_list:
            pos[i][1] += 4
    

    这会将第二组中的节点向上移动。

    你的整个代码:

    import networkx as nx
    import matplotlib.pyplot as plt
    
    G = nx.Graph()
    target_word_list = ["dog", "cow", "cat"] # represented by red nodes
    attribute_word_list = ["car", "pen","paper", "cup"] # represented by blue nodes
    word_list = target_word_list + attribute_word_list
    
    for i in range(0, len(word_list)):
        G.add_node(i)
    
    pos=nx.spring_layout(G) # positions for all nodes
    
    # if node is in second group, move it up
    for i in range(0, len(word_list)):
        if word_list[i] in attribute_word_list:
            pos[i][1] += 4
    
    # draw nodes
    nx.draw_networkx_nodes(G,pos,
                           nodelist=range(0, len(target_word_list)),
                           node_color='r',
                           node_size=50, alpha=0.8)
    nx.draw_networkx_nodes(G,pos,
                           nodelist=range(len(target_word_list), len(word_list)),
                           node_color='b',
                           node_size=50, alpha=0.8)    
    labels = {}
    for idx, target_word in enumerate(target_word_list):
        labels[idx] = target_word
    for idx, attribute_word in enumerate(attribute_word_list):
        labels[len(target_word_list)+idx] = attribute_word
    nx.draw_networkx_labels(G,pos,labels,font_size=14)
    
    plt.axis('off')
    plt.show()
    

    输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-28
      • 1970-01-01
      • 2023-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-29
      • 2012-11-11
      相关资源
      最近更新 更多