Print Assigned Graph Color Name - Networkx

205 views Asked by At

In the code below, I've assigned a color (as a numerical value, not a color name) to a node based on vertex coloring rules. Subsequently, I plot the graph and display the colors. Last, I print the associated numerical color to the node. However, I want to actually print the "name" of the color that is shown in the plot. How can I convert the color number to a color name?

  1. Assign colors to nodes based on vertex coloring rules
%matplotlib inline
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np

# get the maximum vertex degree
max_deg = max(G.degree())
max_deg = max_deg[1]
print('maximum degree = {}'.format(max_deg))

# Create the set of possible colors.
num_colors = max_deg+1
colors = set(range(num_colors))

# We'll store the color of each node as an integer property called 'color'.  Start by initializing
# all the colors to -1 (meaning not yet colored)
for v in complete_graph.nodes():
    complete_graph.nodes[v]['color'] = -1
    
# loop over all nodes
for v in complete_graph.nodes():
    
    print('processing node {}'.format(v))
    
    # copy the possible colors
    possible = colors.copy()
    
    # find colors of neighbors
    nbrs = set([complete_graph.nodes[w]['color'] for w in complete_graph.neighbors(v) if complete_graph.nodes[w]['color']!= -1])
    
    # remove neighbor colors from the set of possible colors
    possible -= nbrs
    
    # pick a color for the current node
    c = next(iter(possible))
    
    print('  neighbor colors: ', nbrs)
    print('  possible colors: ', possible)
    print('  selected color:  ', c)
    
    #update the color
    complete_graph.nodes[v]['color'] = c

Example output for a complete graph with 9 nodes (only Node 0 is printed below):

  processing node 0
  neighbor colors:  set()
  possible colors:  {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
  selected color:   0
  1. Plot the complete graph and color the vertices based on the assigned color numbers
plt.figure(figsize=(5,5))
nx.draw(complete_graph,pos,node_color=[d['color'] for v,d in complete_graph.nodes(data=True)],width=2,cmap=plt.cm.Set3)

Output: Complete Graph

  1. Print node and associated color number
print(complete_graph.nodes(data=True))
[(0, {'color': 0}), (1, {'color': 1}), (2, {'color': 2}), (3, {'color': 3}), (4, {'color': 4}), (5, {'color': 5}), (6, {'color': 6}), (7, {'color': 7}), (8, {'color': 8}), (9, {'color': 9})]

I would prefer it to read:{'color': Green)

1

There are 1 answers

1
Gealber On BEST ANSWER

Well first the you solve that issue just modifying one line, the in wich you declare the colors.

colors = set(range(num_colors))

to this line instead

colors = {'red', 'blue', 'green'}

I imagine that this is not what you want, given that you may want to get the "human"(to say it in a way) representation of the colors or maybe the RGB of this colors. But here's the problem that I found, when we use nx.draw to represent the node, and pass it the nodes_color parameter, internally it uses matplotlib.scatter. Read this from the documentation:

node_color (color or array of colors (default=’#1f78b4’)) – Node color. Can be a single color or a sequence of colors with the same length as nodelist. Color can be string, or rgb (or rgba) tuple of floats from 0-1. If numeric values are specified they will be mapped to colors using the cmap and vmin,vmax parameters. See matplotlib.scatter for more details.

So in a way that's out of my knowledge, networkx uses matplotlib.scatter to transform that numerics values into color.

So I only see two options, one you declare the colors explicitly like I said before, or second you find out how matplotlib.scatter make that transformation.