Error with layouts

I am using igraph through python and am trying to get my network into a layout but I keep running into an error.

Here is my code:

import igraph as ig
import leidenalg as la

leidentest = ig.read("LeidenTestPajek.net")
optimiser = la.Optimiser()

partition = la.ModularityVertexPartition(leidentest)
diff = 1
while diff > 0:
    diff = optimiser.optimise_partition(partition, n_iterations=-1)

print(partition)

ig.plot(partition)

When I do this, an image is created but, because there’s 6000 nodes, it’s impossible to read. So I am attempting to apply a layout to the partition that is created and I can’t get anything to work. If I add a line before:

layout = ig.layout_kamada_kawai_3d()
ig.plot(partition, layout = layout)

I receive this error: AttributeError: module 'igraph' has no attribute 'layout_kamada_kawai_3d', which occurs for every single layout type. I’ve tried installing pretty much everything, pycairo, cairocffi and I can’t get it to work.

The layout functions are methods of the Graph object, which in your case is leidentest. You should therefore call, for example leidentest.layout_kamada_kawai(). You also need to pass it then to the plotting function, i.e.

layout = leidentest.layout_kamada_kawai()
ig.plot(partition, layout=layout)

Note that the plotting function automatically already does a standard layout of the graph. You hence may need to tweak your visualization, and try various layouts. You may also want to reduce the node sizes, making the visualization easier to read.

Thanks for your reply, I managed to get it to work with the exact code you wrote above.

The issue I’m having now is that the partition saved output sorts the ids into groups (community detection algorithm) and they aren’t being applied to the graph. I think I have to convert it into vertex attributes, which has been a struggle.

I am not entirely sure if I understand what you mean. If you want the community identifier (i.e. a number) for each node, this is available from partition.membership. You can store this in a vertex attribute as leidentest.vs['community'] = partition.membership. I am not sure what you mean with “applied to the graph”, but perhaps this gives you enough to go on?

That’s exactly it, sorry I’m new to coding and python so my attempts at explaining my problem are feeble at best.

I just need to figure out how to color the nodes by membership now, thanks for all of your help.