Skip to content Skip to sidebar Skip to footer

Drawing Colored Trees With NetworkX

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 ha

Solution 1:

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)

Post a Comment for "Drawing Colored Trees With NetworkX"