如何基于 networkx 绘制具有不同边宽的网图定向图?

How to plot a netgraph directed plot with varying edge width based on networkx?

我想在 netgraph based on a networkx 网络的网络可视化中设置不同的边缘宽度。这该怎么做? 我正在使用 netgraph,因为据我所知,这是唯一一个在两个节点之间显示两个独立箭头的图形包。到目前为止我的代码(poolsprocesses_weight 都是 dict):

import networkx as nx
import netgraph

network = nx.MultiDiGraph()

# Add node for each pool
for pool in pools_weight.keys():
        network.add_node(pool, size = pools_weight[pool])
# For process between pools, add an edge
for pool in processes.keys():
    for to_pool in processes[pool].keys():
        network.add_edge(pool, to_pool, weight = process_weight[pool][to_pool])

    
# Get positions for the nodes in G
pos_ = pool_pos #nx.spring_layout(network)

netgraph.draw(network, pos_, node_size=50 ,node_color='w', edge_color='k', edge_width=10.0)
plt.show()

如何根据我的 networkx 网络设置不同的边缘宽度?

基于documentation,您可以使用关键字参数edge_width并传递一个由边键控的字典,使每条边具有不同的边权重。这是 draw_edges() 函数文档中从 draw 函数显式调用的部分。

edge_width : float or dict (source, key) : width (default 1.)
        Line width of edges.
        NOTE: Value is rescaled by BASE_EDGE_WIDTH (1e-2) to work well with layout routines in igraph and networkx.

因此,如果您的 networkx 图 network 中有一个名为 weight 的边属性,您可以将调用更改为 netgraph.draw,如下所示:

netgraph.draw(network, pos_, node_size=50 ,node_color='w', edge_color='k',
edge_width={(u,v):weight for u,v,weight in network.edges(data='weight')})