> I would like to add my graph to a matplotlib figure but don't know how. Could somebody please email me a code snippet showing how to do this? Or, if this is not possible, how can you add titles, etc to an igraph.plot object?
As for matplotlib, I don't know the answer off the top of my head, but I guess it can be done with matplotlib's Cairo backend. igraph's plot() function and the Plot() class supports a keyword argument named "target", which can accept an arbitrary Cairo surface to plot on. If you can get the Cairo surface matplotlib plots on somehow, you can construct a Plot instance in igraph using this surface, call the .add() method of the Plot instance to add your graph to the plot and there you go. Alternatively, you can construct a Plot instance on its own and pass its .surface attribute to matplotlib if matplotlib supports that.
Another option I can think of right now for plotting a title to an igraph.plot object is to implement a plottable Title class yourself. If you dig deeper into igraph's drawing internals (in the API documentation of the Plot class), you will find that you can add arbitrary objects to a Plot instance, and igraph will call the __plot__ method of these objects when the plot is drawn. So, if you do something like this:
class Label(object):
def __init__(self, text):
self.text = text
def __plot__(self, context, bbox, palette):
# Call some Cairo methods on the given Cairo context to draw self.text
# into the bounding box given by `bbox`
[...]
then the following might work:
plot = Plot(bbox=(600, 600))
plot.add(your_graph)
title_label = Label("here comes the title of the plot")
plot.add(title_label, bbox=BoundingBox(0, 0, 600, 20))
plot.save()
I've never tried it, but if you manage to come up with a solution, please let me know and I will add it to igraph 0.6.
--
Tamas
_______________________________________________
igraph-help mailing list
igrap...@nongnu.org
http://lists.nongnu.org/mailman/listinfo/igraph-help
> I would like to add my graph to a matplotlib figure but don't know how.
I played around a bit with matplotlib today and this is what I came up
with (see the attachment). It works only for Cairo-based Matplotlib
backends as igraph supports plotting on Cairo contexts only.
--
Tamas
RendererCairo instance has no attribute 'ctx'
Hmmm, 'ctx' is an undocumented attribute of RendererCairo that should
hold the Cairo context on which matplotlib is drawing. It worked for me
with my version of Matplotlib (0.98.5.2), it looks like they have
changed something between Matplotlib 0.98.5.2 and 0.99.3. I'll try to
get hold of the most recent version and see what the new name of this
attribute is. Maybe try replacing 'renderer.ctx' with 'renderer.gc.ctx'
first, this seems to work in 0.99.1
--
Tamas
visual_style = {}
visual_style["vertex_size"] = [20] * ego.vcount()
visual_style["layout"] = layout
visual_style["bbox"] = (800, 800)
visual_style["margin"] = 20
visual_style["vertex_label"] = ['' for i in range(ego.vcount())]
fig = igraph.plot(ego, **visual_style)
but when I pass the visual_style dict to the matplotlib renderer like this (based on your codesnippet):
graph_artist = GraphArtist(ego, (10, 10, 550, 430), **visual_style)
then the dict is not interpreted and the visual styles do not have any effect.
best,
Diederik
self.graph.__plot__(renderer.gc.ctx, self.bbox, self.palette)
The replacement should be as follows:
self.graph.__plot__(renderer.gc.ctx, self.bbox, self.palette, *self.args, **self.kwds)
--
T.