4

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).

enter image description here

However if I change the colormap, the colors DO change. For example:

enter image description here

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.

enter image description here

Any ideas what I'm doing wrong here?

Community
  • 1
  • 1
Wapiti
  • 1,851
  • 2
  • 20
  • 40
  • A self-contained example would help, as well as knowing what `nodecolors` and `x` are in your function. – John Lyon Jan 08 '15 at 00:24
  • Added all the other pieces involved. (I only omitted them to begin with because they are a bit involved and I am new to NetworkX, so thought I might be making a newbie error.) I think this is as self-contained as I can make the question! – Wapiti Jan 08 '15 at 00:48
  • Can you add the output for some other values of "time"? – Joel Jan 08 '15 at 00:56
  • There you go, did times 0,1 and 2. – Wapiti Jan 08 '15 at 01:03
  • I've completely rewritten the question and made it much simpler. I hope this helps to identify what is actually going on. Nobody needs to know about all that other stuff. – Wapiti Jan 08 '15 at 20:01

1 Answers1

3

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 and vmax parameters:

nx.draw_networkx_nodes(G,pos, nodelist, cmap=plt.get_cmap('Set3'),
                       node_color=[0,1], vmin=0, vmax=100)



A detailed explanation:

From the description of the node_color parameter in the draw_networkx_nodes() docs:

If numeric values are specified they will be mapped to colors using the cmap and vmin,vmax parameters. See matplotlib.scatter for more details.

Unfortunately, these docs don't do a good job of describing the behaviour of vmin and vmax. But the reference for matplotlib.scatter does cover it more thouroughly:

vmin and vmax are used in conjunction with norm to normalize luminance data. If either are None, the min and max of the color array is used.

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:

  • [0, 0.1] → [0, 1]
  • [0, 1000] → [0, 1]
  • [0, 200, 1000] → [0, 0.2, 1]
  • [10, 15, 20] → [0, 0.5, 1]
  • [0, 0] → [0,0] (or it could be [1,1]; I didn't actually run this)
Lack
  • 1,625
  • 1
  • 17
  • 29
  • `vmax` and `vmin` are the answer, for some reason I don't comprehend. Your answer does not explain why I get the same results when I say `node_color = [0,1]` and `node_color=[0,.1]` (unless I missed something). HOWEVER, when I set `vmax=1` and `vmin=0`, the colormap suddenly works. This suggests there is something a little off with the defaults? Thanks for putting me on the right track. Incidentally, I asked a very similar question here: http://stackoverflow.com/questions/27871827/colormap-not-working-in-networkx If you would like to answer it as well, I would be happy to accept. – Wapiti Jan 10 '15 at 06:49
  • I edited the answer to add some examples, but I might have misunderstood what you're confused about. I think the colormap *does* "work" -- it's just that *by default, `vmax` is set to the maximum of `node_color`*. – Lack Jan 10 '15 at 07:08