Niklas Braun
Niklas Braun

Reputation: 413

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?

Upvotes: 4

Views: 4334

Answers (2)

Puco4
Puco4

Reputation: 643

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

Upvotes: 1

Paul Brodersen
Paul Brodersen

Reputation: 13041

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')

Upvotes: 5

Related Questions