3

I'm trying to model voting dynamics on networks, and would like to be able to create a graph in NetworkX where I can iterate the voter process on nodes, having their colour change corresponding to their vote 'labels'.

I've managed to get this code to let me see the attributes for each node, but how do I go about using those in a for loop to designate colour?

H = nx.Graph()

H.add_node(1,vote='labour')

H.add_node(2,vote='labour')

H.add_node(3,vote='conservative')

h=nx.get_node_attributes(H,'vote')

h.items()

Gives me the result:

[(1, 'labour'), (2, 'labour'), (3, 'conservative')]

I've got a for loop to do this type of colour coding based on the node number as follows, but haven't managed to make it work for my 'vote' status.

S=nx.star_graph(10)
colour_map=[]
for node in S:
     if node % 2 ==0:
         colour_map.append('blue')
     else: colour_map.append('yellow')
nx.draw(S, node_color = colour_map,with_labels = True)
plt.show()
Hannah
  • 33
  • 1
  • 3
  • Since you mention voter *dynamics*, I guess animation of the node states may be of interest. See http://stackoverflow.com/q/13437284 (networkx-specific) and also http://stackoverflow.com/q/11874767 (matplotlib, not specific to networkx) – Bonlenfum Jan 26 '17 at 14:29
  • I haven't quite gotten that far yet but yes, I probably will be animating this stuff a bit later on, thanks! – Hannah Jan 26 '17 at 15:09

1 Answers1

4

You can iterate the node attributes with H.nodes(data=True) which returns the node name and the node attributes in a dictionary. Here's a full example using your graph.

import networkx as nx
import matplotlib.pyplot as plt

H = nx.Graph()
H.add_node(1, vote='labour')
H.add_node(2, vote='labour')
H.add_node(3, vote='conservative')

color_map = []
for node, data in H.nodes(data=True):
    if data['vote'] == 'labour':
        color_map.append(0.25)  # blue color
    elif data['vote'] == 'conservative':
        color_map.append(0.7)  # yellow color

nx.draw(H, vmin=0, vmax=1, cmap=plt.cm.jet, node_color=color_map, with_labels=True)
plt.show()

This code will draw a different layout of nodes each time you run it (some layouts, as e.g. draw_spring, are available here).

Regarding colors, I use 0.25 for blue and 0.7 for yellow. Note that I use the jet matplotlib colormap and that I set vmin=0 and vmax=1 so that the color values are absolute (and not relative to eachother).

Output of the code above:

enter image description here

UPDATE:

I wasn't aware that you could simply use color names in matplotlib. Here's the updated for loop:

for node, data in H.nodes(data=True):
    if data['vote'] == 'labour':
        color_map.append("blue")
    elif data['vote'] == 'conservative':
        color_map.append("yellow")

And the updated draw command:

nx.draw(H, node_color=color_map, with_labels=True)

Note that this way you get different shades of blue and yellow than in the image above.

edo
  • 1,712
  • 1
  • 18
  • 19
  • That's really helpful, thank you! Out of interest, what's the benefit of / difference between using the numbers, vmax, vmin and cmap=plt.cm.jet that you used compared to 'blue' and 'yellow' colour mapping? – Hannah Jan 26 '17 at 15:18
  • I used this method because that what I used in the past. I tested using color names and it works fine (see my updated answer). – edo Jan 26 '17 at 17:31