我尝试在 IPython 笔记本中进行基本的 igraph 绘图:
fn = 'misrables.gml'
gr = igraph.Graph.Read_GML(fn)
igraph.plot(gr)
我没有看到图表,而是看到了这个:
Out[7]: <igraph.drawing.Plot at 0x1120d6978>
我如何说服 IPython 实际显示图形?
一些说明
内联 matplotlib
工作正常,所以这不是问题。
我安装了cairo
:
import cairo
cairo.version
输出:1.10.0
在绘图对象上调用 _repr_svg()
会导致错误:
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'
最佳答案
您的计算机上是否安装了 Cairo 库及其 Python 绑定(bind)? igraph 的 Plot
对象有一个 _repr_svg_
方法应该足以让 IPython 显示绘图,但是 _repr_svg_
在幕后使用 Cairo 来绘制将图形转换为 SVG 文件,所以如果你没有 Cairo,_repr_svg_
可能会抛出一个 IPython 吞下的异常,然后你会得到一个普通的 repr()
表示情节。
更新:根据更新后的帖子,原来的问题是 _repr_svg_
没有为 Python 3.x 准备,因为它使用了 StringIO
对象,其中 BytesIO
对象是合适的。将函数开头的 io = StringIO()
替换为 io = BytesIO()
并将 return io.getvalue()
替换为 return io.getvalue().decode()
最后解决了问题。 (from io import BytesIO
也需要在 igraph/drawing/__init__.py
的顶部)。 bug report已在 GitHub 上归档;该问题将在下一个次要版本中修复。
更新:该问题现已在开发版中修复;见this commit一个补丁。
关于graphics - igraph 绘图函数不在 IPython 笔记本中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26887513/