在ipython中使用igraph绘制顶点标签的问题 [英] Issue plotting vertex labels using igraph in ipython

查看:784
本文介绍了在ipython中使用igraph绘制顶点标签的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常在IPython笔记本中工作,我在Windows上使用命令打开

I usually work in a IPython notebook, which I open on Windows with the command

ipython qtconsole --matplotlib inline

我目前正在使用IPython QtConsole 3.0.0,Python 2.7.9和IPython 3.0.0。

I'm currently using IPython QtConsole 3.0.0, Python 2.7.9 and IPython 3.0.0.

我想绘制一个图表及其标签

I want to plot a graph, together with its labels

from igraph import *
g = Graph.Lattice([4,4],nei=1,circular=False)
g.vs["label"]=[str(i) for i in xrange(16)]
plot(g, layout="kk")

这样,我获取图表的内联图,但有没有标签,我为每个丢失的标签收到以下消息错误

In this way, I obtain a inline plot of the graph, but there are no labels and I get the following message error for each of the missing labels

link glyph0-x hasn't been detected!

其中x是某个整数。

我还尝试使用 vertex_label = ... 直接在 plot()命令中指定标签,但是没有用。

I also tried to specify the labels directly inside the plot() command, using vertex_label = ..., but nothing works.

在我看来,标签定义正确,问题在于ipython笔记本和/或用于绘制图形的模块。有人可以帮我解决这个问题吗?

It seems to me that the labels are defined correctly and that the problem reside in the ipython notebook and/or in the modules it uses to plot the graph. Can anyone kindly help me with this issue?

我还尝试了所有可能的数字格式SVG和PNG,使用下面的命令,但问题仍然存在。

I also tried all possible figure formats SVG and PNG, using the commands below, but the problem remains.

%config InlineBackend.figure_format = 'svg'
%config InlineBackend.figure_format = 'png'


推荐答案

这个问题可能存在于Qt及其SVG实现的内部深处。将图形格式设置为 png 没有用,因为igraph仅提供图形对象的SVG表示,因此我怀疑IPython首先创建SVG表示,然后将其栅格化为PNG。问题只能通过在 igraph / drawing / __ init __。py 中修补 Plot 类来解决。必须从类中删除 _repr_svg _ 方法,并添加以下方法:

The issue probably lies somewhere deep within the guts of Qt and its SVG implementations. Setting the figure format to png does not help because igraph provides an SVG representation of graph objects only, so I suspect that IPython creates the SVG representation first and then rasterizes it into PNG. The problem can only be solved right now by patching the Plot class in igraph/drawing/__init__.py; one has to delete the _repr_svg_ method from the class and add the following method instead:

def _repr_png_(self):
    """Returns a PNG representation of this plot as a string.

    This method is used by IPython to display this plot inline.
    """
    # Create a new image surface and use that to get the PNG representation
    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(self.bbox.width),
                                 int(self.bbox.height))
    context = cairo.Context(surface)
    # Plot the graph on this context
    self.redraw(context)
    # No idea why this is needed but Python crashes without this
    context.show_page()
    # Write the PNG representation
    io = BytesIO()
    surface.write_to_png(io)
    # Finish the surface
    surface.finish()
    # Return the PNG representation
    return io.getvalue()

我在igraph的Python界面的官方代码中做这个修改有点不安; SVG表示通常更好(和可扩展),但它似乎也导致Windows和Mac OS X上的问题。如果读这篇文章的人对Qt及其SVG实现有更多的经验,我会很感激帮助找到这个bug的根本原因,这样我们就可以在igraph中保留SVG的情节表示。

I'm a bit uneasy to do this modification in the official code of the Python interface of igraph; SVG representations are in general much nicer (and scalable), but it seems like it's causing problems on Windows and Mac OS X as well. If anyone reading this post has more experience with Qt and its SVG implementation, I'd appreciate some help to find the root cause of this bug so we could keep the SVG representation of plots in igraph.

这篇关于在ipython中使用igraph绘制顶点标签的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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