Error creating vertex labels using igraph in ipython

I usually work in an IPython notebook which I open in Windows with the command

ipython qtconsole --matplotlib inline

      

I am currently using IPython QtConsole 3.0.0, Python 2.7.9 and IPython 3.0.0.

I want to plot a graph along with my 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")

      

So I am getting inline plot 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!

      

where x is some integer.

I also tried to specify labels directly within the command plot()

using vertex_label = ...

, but nothing seems to work.

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

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

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

      

+3


source to share


1 answer


The problem probably lies somewhere deep within the Qt whales and its SVG implementations. Setting the drawing format to png

does not help because igraph only provides an SVG representation of graph objects, so I suspect that IPython first creates an SVG representation and then rasterizes it to PNG. The problem can only be solved by fixing the class Plot

in igraph/drawing/__init__.py

; you need to remove the method _repr_svg_

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()

      



I am slightly unable to make this change in the official Python interface code for igraph; SVG views are generally much nicer (and scaled), but this seems to cause problems on both Windows and Mac OS X. If anyone reading this post has more experience with Qt and its SVG implementation, I would grateful for your help in finding the root cause of this error so that we can keep the SVG representation of the graphs in igraph.

+3


source







All Articles