pythonmatplotlibnetworkxnetgraph

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


I would like to set different edge width in a network visualization with netgraph based on a networkx network. How to do this? I am using netgraph as, to my knowledge, this is the only graph package to show two separate arrows in between two nodes. My code so far (pools and processes_weight are both 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()

enter image description here

How can I set different edge width based on my networkx network?


Solution

  • Based on the documentation you can use the keyword argument edge_width and pass a dict keyed by the edges to have different edge weights for each edge. Here's the portion from the draw_edges() function documentation that gets called explicitly from the draw function.

    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.
    

    So if you have an edge attribute named weight in your networkx graph network, you can change your call to netgraph.draw as follows:

    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')})