Why this simple code is not showing plotted the graph?

Question:

I’m trying to follow a simple tutorial on how the igraph library works but the first piece of code didn’t show me a plotted graph as it should. I do have everything installed with pip. The code runs fine but doesn’t show me anything.

from igraph import *

g = Graph()
g.add_vertices(4)
g.add_edges([(1, 2), (2, 3), (0, 3)])
plot(g)
Asked By: Allan Campos

||

Answers:

Not a perfect answer, since it should work and it doesn’t, and I don’t know how to make it work as is. You’ll find some answers for similar question explaining how to fix (or install) cairo, so that it shows.

But I am used to avoid the problem by using matplotlib. Which usually works on all machines/OS.

So

from igraph import *
import matplotlib.pyplot as plt

g = Graph()
g.add_vertices(4)
g.add_edges([(1, 2), (2, 3), (0, 3)])
fig, ax=plt.subplots()
plot(g, target=ax)
plt.show()

So, it’s your code; but it creates a matplotlib figure and axes, and then ask igraph to plot on this axes. Then plt.show actually displays the window.

Answered By: chrslg
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.