问题
I try to do a basic igraph plotting in an IPython notebook:
fn = 'misrables.gml'
gr = igraph.Graph.Read_GML(fn)
igraph.plot(gr)
instead of seeing a graph I see this:
Out[7]: <igraph.drawing.Plot at 0x1120d6978>
How do I persuade IPython to actually show the graphics?
Some clarifications
Inline matplotlib works fine, so this is not the issue.
I have cairo installed:
import cairo
cairo.version
output: 1.10.0
Calling _repr_svg() on the plot object results in an error:
TypeError Traceback (most recent call last)
/Users/boris/Documents/workspace/communityAnalysis/src/utils.py in <module>()
----> 1 z = ig.plot(gr)._repr_svg_()
/usr/local/lib/python3.4/site-packages/igraph/drawing/__init__.py in _repr_svg_(self)
362 # No idea why this is needed but python crashes without
363 context.show_page()
--> 364 surface.finish()
365 # Return the raw SVG representation
366 return io.getvalue()
TypeError: string argument expected, got 'bytes'
回答1:
Do you have the Cairo library and its Python bindings installed on your machine? The Plot object of igraph has a _repr_svg_ method that should be enough for IPython to show the plot, but _repr_svg_ uses Cairo behind the scenes to draw the graph into an SVG file, so if you don't have Cairo, _repr_svg_ probably throws an exception that IPython swallows, and then you get an ordinary repr() representation for the plot.
Update: based on the updated post, it turned out that the problem was that _repr_svg_ was not prepared for Python 3.x as it used a StringIO object where a BytesIO object would have been appropriate. Replacing io = StringIO() with io = BytesIO() at the beginning of the function and replacing return io.getvalue() with return io.getvalue().decode() at the end solves the problem. (from io import BytesIO is also needed at the top of igraph/drawing/__init__.py). A bug report has been filed on GitHub; the issue will be fixed in the next minor release.
Update: the issue is now fixed in the development version; see this commit for a patch.
回答2:
Update for igraph 0.7.1 in python 3.7.4 and IPython 7.8.0 if you install the package with:
conda install python-igraph
Then you can simply use
igraph.plot(gr).show()
and in Ubuntu a .png will be displayed automatically in a separate window with ImageMagick
来源:https://stackoverflow.com/questions/26887513/igraph-plot-function-does-not-show-images-in-an-ipython-notebook