Coloring nodes with RGB pixel values in networkX.d

2019-08-08 15:09发布

问题:

l randomly generated a graph of 16 nodes. Each nodes take a clor : red,blue or green.

Here is my code :

G= nx.watts_strogatz_graph(16, 2, 0, seed=seed) 
color = ['red', 'blue', 'green']
x = G.nodes()
l = len(x)
color_gen = choices(color, k=l)
node_positions = nx.spring_layout(G, scale=100)
nx.draw(G, pos=node_positions, node_color=color_gen, with_labels=True)

such that color_gen represents the color of each node :

['red',
 'blue',
 'blue',
 'red',
 'green',
 'blue',
 'green',
 'blue',
 'red',
 'green',
 'red',
 'blue',
 'red',
 'green',
 'green',
 'green']

Now l would like to change the intensity of each color given the original color. Hence l defined 3 vectors of different intensities for each RGB color as follow :

pixel_red_color=[[255,160,122],[250,128,114],[233,150,122],[240,128,128],[205,92,92],[220,20,60],[178,34,34],[139,0,0],
                 [128,0,0],[255,99,71],[255,69,0]]
pixel_green_color=[[124,252,0],[127,255,0],[50,205,50],[34,139,34],[0,128,0],[0,100,0],[173,255,47],
                   [154,205,50],[0,255,127],[0,250,154],[144,238,144],[107,142,35],[46,139,87]]
blue_pixel_color=[[176,224,230],[173,216,230],[135,206,250],[135,206,235],[0,191,255],[30,144,255],[100,149,237],[70,130,180],[95,158,160],
                  [65,105,225],[0,0,205],[0,0,139],[0,0,128],[25,25,112]]

and l updated the color of each node as follow :

for v in G.nodes():

    if color_gen=='red':
        c = choices(pixel_red_color, k=1)

        color_gen[v] = c[0] # c[0] in order to return [255,2,115] rathet than [[255,2,115]]

    elif color_gen=='green':
        c = choices(pixel_green_color, k=1)
        color_gen[v] = c[0]
    else :
        c= choices(blue_pixel_color, k=1)
        color_gen[v]=c[0]

Then, color_gen becomes :

olor_gen Out[22]:

[[70, 130, 180],
 [135, 206, 250],
 [65, 105, 225],
 [95, 158, 160],
 [0, 0, 139],
 [176, 224, 230],
 [25, 25, 112],
 [0, 0, 205],
 [135, 206, 250],
 [65, 105, 225],
 [0, 0, 139],
 [70, 130, 180],
 [70, 130, 180],
 [70, 130, 180],
 [100, 149, 237],
 [0, 191, 255]]

What l get when l feed nx.draw() with pixel_values of colors ?

nx.draw(G,pos=node_positions, node_color=color_gen, with_labels=True)

l get the following error :

    raise ValueError(msg.format(c.shape, x.size, y.size))
AttributeError: 'list' object has no attribute 'shape'

What l have tried ?

color_gen=np.asarray(color_gen)

then try again :

nx.draw(G,pos=node_positions, node_color=color_gen, with_labels=True)

l get the following error :

python3.6/site-packages/matplotlib/axes/_axes.py", line 3988, in scatter
    raise ValueError(msg.format(c.shape, x.size, y.size))
ValueError: c of shape (16, 3) not acceptable as a color sequence for x with size 16, y with size 16