在 ipython 中使用 igraph 绘制顶点标签时出现问题

ale*_*zok 5 plot svg matplotlib ipython igraph

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

ipython qtconsole --matplotlib inline
Run Code Online (Sandbox Code Playgroud)

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

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

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")
Run Code Online (Sandbox Code Playgroud)

通过这种方式,我获得了图形的内联图,但没有标签,并且对于每个缺失的标签,我收到以下消息错误

link glyph0-x hasn't been detected!
Run Code Online (Sandbox Code Playgroud)

其中 x 是某个整数。

plot()我还尝试使用直接在命令内指定标签vertex_label = ...,但没有任何效果。

在我看来,标签定义正确,问题存在于 ipython 笔记本和/或它用来绘制图表的模块中。谁能帮我解决这个问题吗?

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

%config InlineBackend.figure_format = 'svg'
%config InlineBackend.figure_format = 'png'
Run Code Online (Sandbox Code Playgroud)

Tam*_*más 3

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

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()
Run Code Online (Sandbox Code Playgroud)

在igraph的Python接口官方代码中做这样的修改我有点不太放心;SVG 表示通常更好(并且可扩展),但它似乎也在 Windows 和 Mac OS X 上引起问题。如果阅读这篇文章的人对 Qt 及其 SVG 实现有更多的经验,我希望能得到一些帮助来找到这个错误的根本原因,这样我们就可以在 igraph 中保留绘图的 SVG 表示。