【问题标题】:Large networkx graph for nearest neighbors figure最近邻居图的大型networkx图
【发布时间】:2016-06-16 05:43:55
【问题描述】:

我正在尝试制作网络图。 它基本上是来自具有相应颜色的特定节点的第一个和第二个邻居的子网络。我已经包含了我的函数来创建带有颜色的第一个和第二个邻居以及从这些创建 subgraph列表。

我认为颜色部分可以正常工作,但由于布局太小,我无法确定。我尝试用How to increase node spacing for networkx.spring_layout 扩展spring_layout(G),但它似乎没有用。

我如何创建一个网络图,我可以在其中实际可视化节点及其如此大的连接? (~ 1000 个节点) 我打算添加with_labels,但为了简单起见,没有将其包含在图像中。

我的主要目标是为根节点“c”(青色)、第一个邻居“b”(蓝色)和第二个邻居“g”(绿色)着色,并用一个足够大的数字来读取标签和见连接。

我相信我已经提供了足够的代码,如果我需要添加更多,请告诉我,我会编辑。

import networkx as nx
from collections import defaultdict

def neighborhood(G,node_list):
    #G is the main nx.Graph() object that has ALL the nodes (~7000 nodes)
    D_node_neighborhood = defaultdict(list)
    for node in node_list:
        #Color 1st neighbors blue
        for n1 in G.neighbors(node):
            D_node_neighborhood[node].append((n1,"b"))
            #Color 2nd neighbors green
            for n2 in G.neighbors(n1):
                D_node_neighborhood[node].append((n2,"g"))
    return(D_node_neighborhood)

def subnetwork(G,D_node_neighborhood,root_color = "c"):
    D_node_subgraph = {}
    for node,nghbr_color in D_node_neighborhood.items():
        neighbors = [entry[0] for entry in nghbr_color]
        colors = [entry[1] for entry in nghbr_color]
        H = G.subgraph(neighbors + [node]) #Add root note to neighbors list and create subgraph
        D_node_subgraph[node] = (H,colors + [root_color]) #Do the same with the colors
    return(D_node_subgraph)

D_node_neighborhood.keys()[0] #Grab first one, this object is a list of tuples ("node-name","color-of-node") around 1000
G,colors = D_node_subgraph[node] #Separate them out
nx.draw(G,node_color=colors,node_size=10,alpha=0.8) #Draw the graph w/ corresponding colors

【问题讨论】:

    标签: python matplotlib graph network-programming networkx


    【解决方案1】:

    如果我正确理解您的问题,您希望获得更大的数字。

    你可以在实际绘制后得到图形并增加它的大小。

    nx.draw(G)
    fig = plt.gcf()
    fig.set_size_inches((10, 10))
    plt.show()
    

    为了探索您的图表,我建议使用更适合该任务的工具:Gephi。您可以可视化您的网络并连续应用不同的布局,甚至可以手动移动节点。

    要从 Networkx 转到 Gephi,只需将图形导出为 graphml 格式,如下所示:

    nx.write_graphml(G, 'your_graph.graphml')
    

    【讨论】:

    • 或者你可以通过nx.write_gexf(G, 'your_graph.gexf')去Gephi
    猜你喜欢
    • 1970-01-01
    • 2019-06-05
    • 1970-01-01
    • 2017-06-11
    • 2016-09-19
    • 2018-09-06
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多