R provides a fairly advanced graphing system on its own, and the R interface just uses this, so you can simply create story titles and legends in R. Python does not provide any graphs by default, so igraph uses the Cairo library to draw graphs. However, Cairo is βjustβ a common vector graphics library. This is why you are not getting the same advanced building features in Python.
The plot igraph function creates a plot object in the background, adds a graph that is plotted on the graph, creates a corresponding Cairo surface for it, and then starts drawing a graph on the surface of Cairo. All this happens behind the scenes if you just call plot with the graph as an argument. However, you can create the plot object manually, and then add labels to it before building it, for example:
>>> plot = Plot("plot.png", bbox=(600, 600), background="white")
At this point, you have the plot variable, which is an instance of igraph.drawing.Plot . The plot is reinforced by the surface of the image of Cairo, which has a width of 600 pixels and a height of 600 pixels and which will ultimately be saved in the form with the name plot.png . (You can also put the Cairo surface directly in the first argument of the plot constructor). A call to plot.redraw() will draw the plot, but not save it yet. Calling plot.save() will draw a chart if it has not been drawn, and then save it in the specified file name.
Then you can do two things with the plot:
Add an arbitrary object to the plot that has the __draw__ method. Graph objects have this method, so you can add a graph to the graph as follows:
>>> g = Graph.GRG(100, 0.2) >>> plot.add(g, bbox=(20, 20, 580, 580))
Grab its surface property to access the Cairo surface on which the drawing is being made, build a drawing context for Cairo with that surface, and then draw a graph directly with Cairo using the drawing context.
The second option is how we will add tags to the plot. Fortunately, igraph provides a class called TextDrawer in the igraph.drawing.text package, which helps us a bit with packaging and alignment problems. We just need to create a TextDrawer and then call its draw_at method to add a label to the chart at the given location:
>>> import cairo >>> context = cairo.Context(plot.surface) >>> text_drawer = TextDrawer(context, text="Test label", halign=TextDrawer.LEFT) >>> text_drawer.draw_at(x=100, y=100)
TextDrawer will draw a label with the current Cairo context font, so to set the font used for drawing, you need to use set_font_face , set_font_size and its associated Cairo context methods.
Putting it all together, an example is as follows:
from igraph import Graph, Plot from igraph.drawing.text import TextDrawer import cairo
The example will add a title. Building a legend is more active, but I hope you can continue further on the basis of this idea. Legend labels can be constructed with multiple calls to the draw or draw_at for TextDrawer (after setting the text TextDrawer between calls, of course). You can draw a square around the legend using Cairo's standard challenges. You can also use the node box classes in igraph.drawing.shapes if you want to draw node shapes similar to the ones that igraph uses when drawing a graph.