Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why this simple code is not showing plotted the graph?

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)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading