Set x and y coordinates in python igraph

Asked by Nybi Clamence

Hi,
I am using python 3.2 and igraph 0.6.
I am currently trying to plot the evolution of a subway network year by year, and I'd like to set up the x and y coordinates of each vertex so that the stations remain in the same position as the years go by. Essentially, I wouldn't use one of the built-in layout functions, and since I don't have hundreds of stations, I figure it should be ok.
I simply don't know how I can assign an x and y coordinate, is it simply an attribute as graph.vs[i]["x"]?
Thanks,
Nybi

Question information

Language:
English Edit question
Status:
Solved
For:
igraph Edit question
Assignee:
Tamás Nepusz Edit question
Solved by:
Nybi Clamence
Solved:
Last query:
Last reply:

This question was reopened

Revision history for this message
Tamás Nepusz (ntamas) said :
#1

Yes, that's one way to do it. However, if you want to assign X and Y coordinates to all the vertices at once, it is more efficient to do it as follows:

g.vs["x"] = [1, 2, 3, 4, …]
g.vs["y"] = [5, 6, 7, 8, …]

where the lists contain all the X and Y coordinates in the order of the vertex IDs.

Revision history for this message
Nybi Clamence (nybi) said :
#2

Thanks Tamás Nepusz, that solved my question.

Revision history for this message
Nybi Clamence (nybi) said :
#3

One more thing, how do I get the points to be fixed (not rescaled)? At the moment, I set up my coordinates between -1, 1 for x and y, and the final stage of the network looks great. However, at the beginning of the network, when there are only two stations for instance, the plot rescales itself automatically and the stations take the entire graph.
Thanks.

Revision history for this message
Tamás Nepusz (ntamas) said :
#4

Well, yes, this is an issue that does not really have an elegant solution yet. I usually add four extra nodes in such cases, one in each corner of the plot area, and set their shape to "hidden". Assuming that the extra nodes are the last four nodes in the graph, this can be done as follows:

g.vs["shape"] = "circle" # sets the node shapes to circles
g.vs[g.vcount()-4 : ]["shape"] = "hidden" # sets the shape of the last four nodes to hidden

Revision history for this message
Tamás Nepusz (ntamas) said :
#5

Also, I will add an option in igraph 0.6 that allows the user to disable the layout rescaling and use the layout coordinates directly.

Revision history for this message
Nybi Clamence (nybi) said :
#6

Thanks a lot Tamas. Elegant or not, it worked for me. I actually put the four first nodes as "hidden", which works fine with my algorithm (i.e. I copy an imported graph bit by bit and year and year, so indexing is not so important in this case).
Thanks!