0

For a directed networkx graph the data is read from a csv file; The nodes' colours are set by degree of nodes. I want to set each edge line colour to take the node's colour. There has to be an efficient way to access the indices of nodes and respective edges of each node.

Any ideas to achieve this?

current sample data and code as follows:

import networkx as nx
import matplotlib.pylab as plt
import pandas as pd

G = nx.Graph()
  
edges = [(1, 2, 10), (1, 6, 15), (2, 3, 20), (2, 4, 10), 
         (2, 6, 20), (3, 4, 30), (3, 5, 15), (4, 8, 20),
         (4, 9, 10), (6, 7, 30)]
  
G.add_weighted_edges_from(edges)

def nodescoldeg(g):
   nodecol = [g.degree(u) for u in g]
   return nodecol

#edge_colour can be a sequence of colors with the same length as edgelist
def edgecoldeg(g):
   nodecol = nodecoldeg(g)
   i = 0
   for index1, u in enumerate(g.nodes()):
      for index2, v in enumerate(nx.dfs_edges(g,u)):
         edgecol[i] = nodecol[index1]
         i = i+1
   #??? edgecol gives out of bound error...
   return edgecoldeg

nx.draw(G, node_color=nodecoldeg(G), edge_color=edgecoldeg(G), with_labels=True)
bonCodigo
  • 14,268
  • 1
  • 48
  • 91
  • Why the DFS over edges? Surely, you just need: `for (u, v) in g.edges(): edge_color = g.degree(u)`? – Paul Brodersen Apr 20 '23 at 10:07
  • @PaulBrodersen well 1) there's more than one network to be processed, and actual code has more parameters flowing through; 2) your guess didn't work in `nx.draw...`, 3) plus u bound is smaller than v bound. Any road after posting, I managed to tweak a solution, but there has to be an efficient solution. Do you know how to draw edge line length based on weight/particular value? – bonCodigo Apr 21 '23 at 10:23
  • 1
    https://stackoverflow.com/a/75822721/2912349 – Paul Brodersen Apr 21 '23 at 10:41
  • @PaulBrodersen tried that before, didn't work well for 1000s of nodes...if can avoid subplots by any means, that would be preferred – bonCodigo Apr 21 '23 at 10:50
  • @PaulBrodersen I am considering breaking it down to 100s of nodes. The issues are 1) improving clarity/presentation by increasing space around nodes and edges 2) drawing edge lines without overlapping but as extended branches (trying to do a directed graph left to right, top to bottom form) 3) the integrating weighted edge lines. I am open to try potential suggestions. Thank you. – bonCodigo Apr 21 '23 at 11:07

0 Answers0