5

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

ipython qtconsole --matplotlib inline

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!

where x is some integer number.

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

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?

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'
alezok
  • 151
  • 4

1 Answers1

3

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

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.

Tamás
  • 47,239
  • 12
  • 105
  • 124