0

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?

Paul Brodersen
  • 11,221
  • 21
  • 38

1 Answers1

2

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')})
cookesd
  • 1,296
  • 1
  • 5
  • 6
  • Thanks for the help! How would I also include different sizes of the nodes? – TobiasKAndersen Dec 18 '20 at 16:40
  • 1
    You can pass a dictionary keyed by the nodes to the `node_size` keyword argument. The function documentation points to all the inner functions it uses `draw_edges`, `draw_nodes`, `draw_node_labels`, and `draw_edge_labels` and they explain the possible arguments you can use to modify your graph. – cookesd Dec 18 '20 at 20:35