【问题标题】:Color Nodes According the Communities when using Girvan Newman Algorithm in Python在 Python 中使用 Girvan Newman 算法时根据社区的颜色节点
【发布时间】:2020-07-07 17:52:55
【问题描述】:

在下面的代码中,我想根据 girvan newman 算法给出的社区为节点着色。

G = nx.karate_club_graph()
posi_gn = nx.spring_layout(G)
comp = community.girvan_newman(G)
posi_gn = nx.spring_layout(G)

nx.draw_networkx(G, posi_gn, with_labels=True,  arrows=True, font_color='gray')
plt.show()

【问题讨论】:

    标签: python nodes networkx


    【解决方案1】:

    这取决于您要定义社区的级别,即您要定义多少个社区。知道了这一点,您可以在社区定义的组中绘制节点:

    G = nx.path_graph(10)
    posi_gn = nx.spring_layout(G)
    comp = nx.community.girvan_newman(G)
    
    k = 3   # number of communities
    for _ in range(k-1):
        comms = next(comp)
    
    colors = 'rgb'
    for nodes, c in zip(comms, colors):
        nx.draw_networkx_nodes(G, posi_gn, nodelist=nodes, node_color=[c], with_labels=True, arrows=True, font_color='gray')
    nx.draw_networkx_edges(G, posi_gn)
    

    查看documentation 了解更多信息。

    【讨论】:

    • 感谢您的回答。如果社区是 10 个或 20 个呢?
    • 您只需更改为 10 个社区而不是 3 个。您必须添加更多颜色或使用不同的方法(例如颜色图)添加颜色。你去 10 有什么问题?
    • 每次我都会加载一个新图表,所以我希望颜色与将要生成的元组的数量完全一致。
    • 我不确定我是否理解您的问题。添加更多颜色不会有帮助吗?您可以使用node_colorcmap 参数扩展列表或使用颜色映射方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-03
    • 1970-01-01
    相关资源
    最近更新 更多