Color normalization with increasing number of used colors


I'm using graph-tool to create demonstrations of how a few graph
coloring algorithms work. To do so, I'm creating snapshot images
at each step of the coloring process.

While pinning the vertices' positions is trivial, I couldn't find a
nice solution to "pin the colors": every time a new color is used,
the normalization process picks new colors for all vertices,
giving the false impression that the algorithm has changed them.

The code sample below probably explains the problem better: it
creates files ex-vcolor-{0,1,2}.png showing that the vertex color

g = Graph(directed=False)
v_color = g.new_vertex_property("int")
pos = sfdp_layout(g)
for v in g.vertices():
    v_color[v] = int(v)
    graph_draw(g, vertex_fill_color=v_color, pos=pos,
               output="ex-vcolor-" + str(int(v)) + ".png")

What I would like to have is: use the normalization process only
once for n colors (to pick "spaced" colors) and then stick to them
even when just a few are used in the graph being draw. Is this
possible? Any ideas?


PS. And thanks a lot for graph-tool! It is a real time saver :slight_smile:

The colors do not have to be simple scalar values, they can also be
strings such as "green" or "#00FF00" or RGBA vectors such as [0., 1.,
0., 1.]. That means you can set the color by hand, and avoid the
normalization. For example:

color = g.new_vertex_property("vector<double>")
color[g.vertex(0)] = [.1, .9, .1, 1.]
color[g.vertex(1)] = [.9, .0, .1, 1.]
graph_draw(g, vertex_fill_color=color)

Another option is to construct your own colormap, and pass it as the
vcmap parameter. You can learn more about color maps here:


Thanks! This is exactly what I was looking for.