News

New paper! in the American Naturalist

Wednesday, March 11, 2020

Networkx: how to color nodes by node attributes

import networkx as nx
import matplotlib.pyplot as plt

g = # nx.Graph
attr_ls = dict(g.nodes(data='specific_attribute_name'))
pos = nx.kamada_kawai_layout(g) # or any other layout
nx.draw(g, pos, node_color=list(attr_ls.values()), cmap=plt.cm.coolwarm, node_size=50)

# used the colormap 'plt.cm.coolwarm' in this example.
# the default size of nodes is 300 apparently.
# node_color: a list of numeric attributes for each node in this example.