graphics - igraph 绘图函数不在 IPython 笔记本中显示图像

标签 graphics ipython igraph

我尝试在 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/

相关文章:

c# - 何时处置图形对象

image - JavaFX 更改 imageView 中的图像

ios - OpenGL ES如何在上一帧上方绘制

numpy - 如何使 set_printoptions(suppress=True) 永久化?

ipython - 警告 : Readline services not available or not loaded. AttributeError: 'module' 对象没有属性 'set_completer_delims'

Python igraph : Count distinct edges in a weighted graph

algorithm - 我有一个 OpenGL Tessellated Sphere,我想在其中切一个圆柱形孔

python - Savefig as eps 产生不可使用的 eps

R igraph,如何绘制混合形状和光栅的顶点?

r - 在 igraph 中存储顶点的顺序