igraph plot函数不会在IPython笔记本中显示图像 [英] igraph plot function does not show images in an IPython notebook

查看:803
本文介绍了igraph plot函数不会在IPython笔记本中显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在IPython笔记本上做一个基本的igraph绘图:

  fn ='misrables.gml'$ b $ (gr)



而不是看到一个图,我看到这个:

  Out [7]:< igraph.drawing.Plot at 0x1120d6978> 

我如何说服IPython实际显示图形?



一些说明



内嵌 matplotlib 不是问题。



我已安装 cairo >:

 导入cairo 
cairo.version

输出: 1.10.0



调用 _repr_svg() on plot对象会导致一个错误:

pre $ TypeError Traceback(最近一次调用最后一次)
/ Users / boris / Documents < module>()中的/workspace/communityAnalysis/src/utils.py
----> 1 z = ig.plot(gr)._ repr_svg_()

/usr/local/lib/python3.4/site-packages/igraph/drawing/__init__.py in _repr_svg_(self)
362#不知道为什么需要这个,但python崩溃没有
363 context.show_page()
- > 364 surface.finish()
365#返回原始的SVG表示形式
366返回io.getvalue()

TypeError:预期的字符串参数,得到'bytes'


解决方案

您的计算机上是否安装了Cairo库及其Python绑定? igraph的 Plot 对象有一个 _repr_svg _ 方法,它应该足以让IPython显示该图,但 _repr_svg _ 在幕后使用Cairo将图形绘制成SVG文件,所以如果您没有开罗, _repr_svg _ 可能会抛出这是IPython吞下的一个例外,然后你得到了一个普通的 repr()表示图。



更新:根据更新后的文章,结果发现问题在于 _repr_svg _ 没有为Python 3.x做准备,因为它使用了 StringIO 对象,其中 BytesIO 对象是合适的。在函数的开始处用 io = BytesIO()替换 io = StringIO(),并将返回io.getvalue()返回io.getvalue()。decode()来解决问题。在 igraph / drawing / __ init __。py >的顶部还需要 c 。 GitHub已提交错误报告;该问题将在下一个次要版本中得到解决。



更新:问题现在已在开发版本中修复;请参阅此提交以获得补丁。


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'

解决方案

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.

这篇关于igraph plot函数不会在IPython笔记本中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆