简体   繁体   中英

python igraph: nodes and edges color according to a number associated to the node

I'm really new to python and igraph, so the question may be really easy.

I have a network with 128 nodes and an array of 128 numbers, each between 0 and 1. Numbers represent a feature of nodes.

I want two things:

  • the color of nodes i and j has to be the same if array[i] == array[j]
  • the color of the edge between i and j has to be given according to this criterion: if array[i]<=array[j] -> color_edge[ij] = color_node[i] else color_edge[ij] = color_node[j]

I tried many thing, and I think the major problems are:

  • g.vs[i]["color"] = number doesn't work.I have to insert a string of the type "red" or "HTML notation for color". But how can I associate each number of my array to a string?
  • g.es["color"] gives a color to all links. However g.es[i]["color"] doesn't work (here g is my graph and in g.es[i][".."] , i is the ID of the edge).

About the first question, the only solution I can propose is to assign MANUALLY a color to each node like

g.vs[0]["color"] = "red" . . . g.vs[127]["color"] = "blue"

then check the condition on array and eventually change the color of one of the two nodes ( g.vs[i]["color"] = g.vs[j]["color"] ). But it's a huge loss of time and is not a general way to do that (what if the network has 10^6 nodes?). I was suggested in this here

About the second question, I tried to adapt the code in here to my case but the best I obtained was two different colors (and morover I don't understand how they are "the choosen two")

How can I solve this?

Ps I know I asked two questions, but I think they're very related. However, if necessary, I can edit this post and create a new one.

Thanks in advance.

I asked this question on igrpah mailing list.

To obtain same color of nodes i and j if array[i] == array[j] you can do this:

id_gen = UniqueIdGenerator()
color_indices = [id_gen.add(value) for value in array]
palette = ClusterColoringPalette(len(id_gen))
colors = [palette[index] for index in color_indices]
graph.vs["color"] = colors 

To obtain the color of edges to be determined by the criterion in the question you can do

for edge in graph.es:
u, v = edge.tuple
  edge["color"] = colors[u if array[u] <= array[v] else v]

The full answer I received from the mailing list is here .

Make a dictionary of colors associated with each difference. It's not clear how many colors you need, but assign either a color name "red" or color code "#FFFFFF" to each difference. For example,

coldict = {}
coldict[0] = "red"

So that when you check the difference array[i] - array[j] = 0, then it assigns red. Do this for all expected differences.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM