NOTE -- This is a complete rewrite of the previous question, which I think was too complex. It presents a much simpler version which, if I can solve, will result in progress.
I have adapted my code from this SO answer: Is there a way to guarantee hierarchical output from NetworkX?
With the following very simple code, I get strange coloring behavior.
import networkx as nx
import matplotlib.pyplot as plt
G = nx.DiGraph()
G.add_node("ROOT")
for i in xrange(1):
for j in range(1):
G.add_node(str(j)+"_%i" % i, )
if j ==0:
G.add_edge("ROOT", str(j)+"_%i" % i)
else:
G.add_edge(str(j-1)+"_%i" %i, str(j)+"_%i" % i)
pos=nx.graphviz_layout(G,prog='dot')
for i in xrange(1):
nodelist = ['ROOT']
for j in range(1):
nodelist.append(str(j)+"_%i" % i )
nx.draw_networkx_nodes(G,pos, nodelist=nodelist, cmap=plt.get_cmap('Set3'), node_color=[0,1])
nx.draw_networkx_edges(G, pos,arrows=True)
limits=plt.axis('off')
It seems to NOT matter what values I give for the node_color
, only whether the values are different or not. For example, with node_color = [0,1]
, I get the exact same behavior as when it is [0,.1]
, or [0,1000]
. (Why? The colormap takes values between 0 and 1).
However if I change the colormap, the colors DO change. For example:
And if I set node_color = [3,3]
(or any two values which are the same), I always get the same thing, the nodes are colored identically.
Any ideas what I'm doing wrong here?
Before getting to the colormap, the node color values are normalized to the interval [0, 1]. This is presumably meant to use the full range of colors, regardless of the range of the values given. To use a different interval for scaling, you can set the
vmin
andvmax
parameters:A detailed explanation:
From the description of the node_color parameter in the draw_networkx_nodes() docs:
Unfortunately, these docs don't do a good job of describing the behaviour of
vmin
andvmax
. But the reference for matplotlib.scatter does cover it more thouroughly:So, the minimum of the node_color array you pass is, by default, mapped to 0 on the colormap, and the maximum to 1. Everything in between is mapped to the interval [0.0,1.0] using a linear mapping (this is called normalization or feature scaling). Here are some examples, going from
node_color
to points in the colormap's domain: