1

I am trying to color the nodes in networkx according to node attributes, using a colormap. I was wondering how the middle point of the colormap could be set to zero?

This is an example code that I currently have:

import networkx as nx
from matplotlib import pyplot as plt

g=nx.Graph()

g.add_nodes_from(['A','B','C','D','E'])
g.add_edges_from([('A','B'),('B','C'),('B','D')])

nodes=g.nodes()

success_factor={'A':-1,'B':0,'C':7,'D':-2,'E':6}
nx.set_node_attributes(g, success_factor, 'success_factor')

success_color = [nx.get_node_attributes(g, 'success_factor')[v] for v in g]

pos=nx.spring_layout(g)
plt.figure()

ec = nx.draw_networkx_edges(g, pos=pos, alpha=0.8)
nc = nx.draw_networkx_nodes(g, pos=pos, nodelist=nodes, node_color=success_color, cmap=plt.cm.seismic)

ax = plt.gca()
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="2%", pad=0.05)
plt.colorbar(nc, cax=cax)

plt.show()
  • See https://stackoverflow.com/a/20528097 and other answers to https://stackoverflow.com/q/7404116, also https://stackoverflow.com/a/20146989. (The draw_networkx_... functions use matplotlib) – Bonlenfum Mar 07 '18 at 10:28

0 Answers0