[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [igraph] how to plot graph on matplotlib figure?
From: |
Tamas Nepusz |
Subject: |
Re: [igraph] how to plot graph on matplotlib figure? |
Date: |
Mon, 28 Jun 2010 10:01:10 +0100 |
Hi,
> 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