Svg 在ipython中使用igraph发布打印顶点标签
我通常在IPython笔记本中工作,我用命令在Windows上打开它Svg 在ipython中使用igraph发布打印顶点标签,svg,matplotlib,plot,ipython,igraph,Svg,Matplotlib,Plot,Ipython,Igraph,我通常在IPython笔记本中工作,我用命令在Windows上打开它 ipython qtconsole --matplotlib inline 我目前正在使用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 xrang
ipython qtconsole --matplotlib inline
我目前正在使用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")
通过这种方式,我获得了图形的内联图,但是有没有标签,并且对于每个缺少的标签,我得到了以下消息error
link glyph0-x hasn't been detected!
其中x是一个整数
我还尝试使用vertex\u label=…
直接在plot()
命令中指定标签,但没有任何效果
在我看来,标签定义正确,问题存在于ipython笔记本和/或用于绘制图表的模块中。有人能帮我解决这个问题吗
我还使用下面的命令尝试了所有可能的图形格式SVG和PNG,但问题仍然存在
%config InlineBackend.figure_format = 'svg'
%config InlineBackend.figure_format = 'png'
这个问题可能存在于Qt及其SVG实现的内部深处。将图形格式设置为
png
没有帮助,因为igraph仅提供图形对象的SVG表示,因此我怀疑IPython首先创建SVG表示,然后将其光栅化为png。现在只能通过修补igraph/drawing/\uuuuuu init\uuuuuuuu.py
中的Plot
类来解决此问题;必须从类中删除\u repr\u svg\u
方法,并添加以下方法:
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表示