Consistent colour scale and legend

I'm trying to use graph-tool to track concentrations on a network and was
wondering what the best way would be to obtain consistent colour scales for
a vertex property? I track the concentration of a substance as a vertex
property and then use that as the vertex_fill_color in graph_draw because I
would like to visualise the change in concentration over the network over
time. This works great, but it appears that every time I plot the graph the
colormap is rescaled to the minimum and maximum values in the vertex
property. So if my values range from 0.0 to 0.4 and I use the Blues
colormap 0.0 is white and 0.4 is dark blue. If in the next time step my
values range from 0.0 to 0.6 then 0.6 is dark blue and 0.4 is now a
different shade of blue. Instead, if I know 0.0 would be the minimum value
and 1.0 would be the maximum I would like to consistently plot my graphs
using that scaling, so that 0.4 always has the same shade of blue. I hope
this all made sense.

attachment.html (1.02 KB)

You can use matplotlib and a colormap for this. I think you need to create
a vp that's only for the colors, the documentation says which types are
accepted for colors. You can get a color with a cmap by pasing a value from
0 - 1.

See this example for using a cmap:

import matplotlib.pyplot as plt
cmap = plt.get_cmap('cool')
(0.40000000000000002, 0.59999999999999998, 1.0, 1.0)

attachment.html (2.32 KB)

Cheers, I managed to get the colours consistent with your suggestion. One
more thing I forgot to ask in my original post, is there a way to display a
colour legend with the graph plot?

attachment.html (3.48 KB)

The only way to do that is to embed the drawing into a matplotlib figure
(via the mplfig parameter of graph_draw()), and then insert a legend
from there.


Thanks for you reply! I'm not sure I really understand how this works
properly, the only example I could find is here:
I tried something similar for my plot:

ax = plt.gca()
gt.graph_draw(g, pos=pos, vertex_fill_color=concentration, output=fname,
                  vcmap=cmap, vertex_shape=groups, vertex_size=7, mplfig=ax)

This results in all my nodes just being black instead of shades of blue as
they are if I don't use mplfig. Additionally, I would like colorbar() to
use the vertex property concentration, but I can't find any information on
how I might be able to do this.

Your help is greatly appreciated!

attachment.html (2.21 KB)

In case that you managed to add a colorbar, would you please post the