Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving iGraph python figures?

I am using igraph to generate network plots using igraph.plot(), but I can only get these to pop up, not actually save without manually saving each figure:

def testplot(graph):
    graph.vs['label'] = graph.vs['name']
    x = plot(graph, vertex_size=[a/5 for a in graph.betweenness()],
             layout = graph.layout('grid'))
    x.show()

When I try to do the following, where plt is matplotlib:

def testplot(graph, name):
    graph.vs['label'] = graph.vs['name']
    igraph.plot(graph, vertex_size=[a/5 for a in graph.betweenness()],
                layout = graph.layout('grid'))
    plt.savefig(name + '_allyBetweenness.png')

It saves an empty picture. Any tips?

like image 844
Niklas Braun Avatar asked Oct 20 '25 03:10

Niklas Braun


2 Answers

igraph.plot does not create a matplotlib figure, so there is nothing to save (unlike other packages such as networkx). This should work:

def testplot(graph, name):
    graph.vs['label'] = graph.vs['name']
    out = igraph.plot(graph, vertex_size=[a/5 for a in graph.betweenness()],
                      layout = graph.layout('grid'))
    out.save(name + '_allyBetweenness.png')
like image 92
Paul Brodersen Avatar answered Oct 24 '25 04:10

Paul Brodersen


You can also save the plot directly with:

igraph.plot(g, "name.png")

For (some) more information see: https://igraph.org/python/doc/tutorial/tutorial.html#saving-plots

like image 44
Puco4 Avatar answered Oct 24 '25 03:10

Puco4