Search code examples
pythoncolorsnetworkx

Networkx, changing node colors


I want to change the color of the node, based on the attribute values. Specifically in this case, when the attribute 'status' is 1, I want the color to be something and when it's 0, some other color. One restriction is that I want the nodes to be printed in a lattice format, where I use 2d_grid_graph.

When graphing this with node_color='blue' or any other color, the output works correctly, but when I use conditionals to change the color according to 'status' it only shows one color.

import numpy as np
import matplotlib.pyplot as plt
from random import randint
import networkx as nx

N = 10 #Lattice Size

#Random Coordinate Generator
def random(m, n):
    seen = set()

    x, y = randint(m, n), randint(m, n)

    while True:
        seen.add((x, y))
        yield (x, y)
        x, y = randint(m, n), randint(m, n)
        while (x, y) in seen:
            x, y = randint(m, n), randint(m, n)


#Generates Lattice
G=nx.grid_2d_graph(N,N)
pos = dict( (n, n) for n in G.nodes() )
labels = nx.get_node_attributes(G, 'status') 
G.add_nodes_from(G.nodes,status='1')

#Change Node attribute
num_nodes=N*N
half=int(num_nodes/2)
decaying = [0]*half
a=random(0,N-1)

for i in range(half):
    cor=next(a)
    decaying[i]=cor
for j in range(half):
    a=decaying[j]
    G.add_node(a, status='0')

node_color = []

#Plot nodes
labels = nx.get_node_attributes(G, 'status') 
nx.draw(G,pos=pos,node_color=node_color, labels=labels,node_size=200)

Solution

  • Something like this could works:

    import networkx as nx
    import matplotlib.pyplot as plt
    M = nx.erdos_renyi_graph(10, 2)
    color = []
    for node in M:
        if node > 1:
            color.append('red')
        else:
            color.append('green')
    nx.draw(M, node_color=color)
    plt.show()
    

    Here the application with your example

    import numpy as np
    import matplotlib.pyplot as plt
    from random import randint
    import networkx as nx
    
    N = 10 #Lattice Size
    
    #Random Coordinate Generator
    def random(m, n):
        seen = set()
    
        x, y = randint(m, n), randint(m, n)
    
        while True:
            seen.add((x, y))
            yield (x, y)
            x, y = randint(m, n), randint(m, n)
            while (x, y) in seen:
                x, y = randint(m, n), randint(m, n)
    
    
    #Generates Lattice
    G=nx.grid_2d_graph(N,N)
    pos = dict( (n, n) for n in G.nodes() )
    labels = nx.get_node_attributes(G, 'status')
    G.add_nodes_from(G.nodes,status='1')
    
    #Change Node attribute
    num_nodes=N*N
    half=int(num_nodes/2)
    decaying = [0]*half
    a=random(0,N-1)
    
    for i in range(half):
        cor=next(a)
        decaying[i]=cor
    for j in range(half):
        a=decaying[j]
        G.add_node(a, status='0')
    
    node_color = []
    for status in  list(nx.get_node_attributes(G, 'status').values()):
        if status == '1':
            node_color.append('red')
        else:
            node_color.append('green')
    #Plot nodes
    labels = nx.get_node_attributes(G, 'status')
    nx.draw(G,pos=pos,node_color=node_color, labels=labels,node_size=200)
    
    plt.show()
    

    enter image description here