【问题标题】:Drawing a large weighted network in networkx based on thickness?根据厚度在networkx中绘制一个大型加权网络?
【发布时间】:2014-02-11 19:26:16
【问题描述】:

如何在networkx中按厚度绘制N>1000个节点的加权网络?如果我有一个源节点、目标节点和每个边的权重的 .csv 列表,并且我正在考虑使用以下方法:

for i in range(N)
G.add_edge(source[i],target[i], weight=weight[i]) 
nx.draw_networkx_edges(G)

但是,我是否必须为每条边缘赋予厚度?还是每组具有相似厚度的边?

【问题讨论】:

    标签: networkx thickness


    【解决方案1】:

    您可以单独指定每条边,或者如果您有一些计算分组的函数(然后对draw_network_edges 进行多次调用),也可以将它们分组定义。

    这是一个随机图示例,它按原样使用边权重,首先定义边粗细,然后使用数据作为着色。

    import matplotlib.pyplot as plt
    import networkx as nx
    import numpy as np
    
    n = 15; m = 40
    
    # select some edge destinations
    L = np.random.choice(xrange(n), 2*m)
    # and suppose that each edge has a weight
    weights = 0.5 + 5 * np.random.rand(m)
    
    # create a graph object, add n nodes to it, and the edges
    G = nx.DiGraph()
    G.add_nodes_from(xrange(n))
    for i, (fr, to) in enumerate(zip(L[1::2], L[::2])):
      G.add_edge(fr, to, weight=weights[i])
    
    # use one of the edge properties to control line thickness
    edgewidth = [ d['weight'] for (u,v,d) in G.edges(data=True)]
    
    # layout
    pos = nx.spring_layout(G, iterations=50)
    #pos = nx.random_layout(G)
    
    # rendering
    plt.figure(1)
    plt.subplot(211); plt.axis('off')
    nx.draw_networkx_nodes(G, pos)
    nx.draw_networkx_edges(G, pos, width=edgewidth,)
    
    plt.subplot(212); plt.axis('off')
    
    # rendering
    nx.draw_networkx_nodes(G, pos)
    nx.draw_networkx_edges(G, pos, edge_color=edgewidth)
    
    plt.show()
    

    这会给你这样的东西:

    显然,您也可以使用更复杂的函数来组装适合您的应用程序的边缘宽度值列表(例如,分箱值或不同属性的乘积)。

    【讨论】:

    • 加个图例怎么样?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 2016-01-12
    • 1970-01-01
    相关资源
    最近更新 更多