将标题和图例添加到igraph图中 [英] Add title and legend to igraph plots
问题描述
在适用于python的igraph中,您可以在图中添加图例和标题吗?据我所知,在手册或教程中都没有提到.但是在R中是可能的.
In igraph for python, can you add a legend and title to a plot? Neither is mentioned in the manual or the tutorial as far as I can see. It is possible in R however.
推荐答案
R本身提供了一个非常高级的绘图系统,而R接口只是利用了这一点,因此您可以在R中简单地创建绘图标题和图例. Python默认情况下不提供任何绘图,因此igraph使用Cairo库绘制图形绘图.但是,开罗只是"一个通用的矢量图形库.这就是为什么您无法在Python中获得相同的高级绘图功能的原因.
R provides a pretty advanced plotting system on its own and the R interface simply makes use of this so that's why you can simply create plot titles and legends in R. Python does not provide any plotting by default, so igraph uses the Cairo library to draw graph plots. However, Cairo is "just" a generic vector graphics library. That's why you don't get the same advanced plotting capabilities in Python.
igraph的plot
函数在背景中创建一个Plot
对象,将要绘制的图形添加到图形本身,为其创建合适的Cairo曲面,然后开始在Cairo曲面上绘制图形.如果您仅使用图形作为参数调用plot
,所有这些操作都会在后台发生.但是,您可以手动创建Plot
对象,然后在对其进行绘制之前为其添加标签,如下所示:
The plot
function of igraph creates a Plot
object in the background, adds the graph being plotted to the plot itself, creates an appropriate Cairo surface for it, and then starts drawing the graph on the Cairo surface. All this happens behind the scenes if you simply call plot
with a graph as an argument. However, you can create a Plot
object manually and then add labels to it before it is being plotted, like this:
>>> plot = Plot("plot.png", bbox=(600, 600), background="white")
At this point, you have a plot
variable, which is an instance of igraph.drawing.Plot
. The plot is backed by a Cairo image surface which is 600 pixels wide and 600 pixels high, and which will eventually be saved into a filed named plot.png
. (You can also supply a Cairo surface directly in the first argument of the Plot
constructor). Calling plot.redraw()
would draw the plot but not save it yet. Calling plot.save()
would draw the plot if it has not been drawn yet and then save it to the given filename.
然后您可以对情节做两件事:
You can then do two things with a plot:
-
向具有
__draw__
方法的绘图中添加任意对象.Graph
对象具有这种方法,因此您可以按如下所示将图形添加到绘图中:
Add an arbitrary object to the plot that has a
__draw__
method.Graph
objects have such a method so you can add a graph to the plot as follows:
>>> g = Graph.GRG(100, 0.2)
>>> plot.add(g, bbox=(20, 20, 580, 580))
利用其surface
属性访问在其上进行绘制的开罗曲面,使用该曲面构造开罗图形上下文,然后使用该绘制上下文直接在开罗上绘制该图.
Grab its surface
property to access the Cairo surface on which the drawing is done, construct a Cairo drawing context with this surface, and then draw on the plot directly with Cairo using the drawing context.
第二个选项是我们要如何向绘图添加标签.幸运的是,igraph在其中提供了一个名为 TextDrawer
的类. igraph.drawing.text
软件包,可以帮助我们解决一些包装和对齐问题.我们只需创建一个TextDrawer
,然后调用其draw_at
方法,即可在给定位置的图上添加标签:
The second option is how we are going to add labels to the plot. Luckily igraph provides a class named TextDrawer
in the igraph.drawing.text
package that helps us a bit with wrapping and alignment issues. We simply have to create a TextDrawer
and then call its draw_at
method to add a label to the plot at a 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
将使用Cairo上下文的当前字体绘制标签,因此您必须使用Cairo上下文的set_font_face
,set_font_size
和相关方法来调整用于绘制的字体.
The TextDrawer
will draw the label with the current font of the Cairo context, so you have to use the set_font_face
, set_font_size
and related methods of the Cairo context to adjust the font that is used for drawing.
将它们放在一起,示例如下:
Putting it all together, the example goes like this:
from igraph import Graph, Plot
from igraph.drawing.text import TextDrawer
import cairo
# Construct the plot
plot = Plot("plot.png", bbox=(600, 650), background="white")
# Create the graph and add it to the plot
g = Graph.GRG(100, 0.2)
plot.add(g, bbox=(20, 70, 580, 630))
# Make the plot draw itself on the Cairo surface
plot.redraw()
# Grab the surface, construct a drawing context and a TextDrawer
ctx = cairo.Context(plot.surface)
ctx.set_font_size(36)
drawer = TextDrawer(ctx, "Test title", halign=TextDrawer.CENTER)
drawer.draw_at(0, 40, width=600)
# Save the plot
plot.save()
该示例将为绘图添加标题.构建图例的工作更多,但我希望您可以根据这个想法继续进行.可以通过重复调用TextDrawer
的draw
或draw_at
方法来构造图例的标签(当然,在两次调用之间调整TextDrawer
的text
属性之后).您可以使用标准的Cairo调用在图例周围绘制一个框.您还可以在 igraph.drawing.shapes
中使用节点抽屉类您想要绘制类似于igraph绘制图形时使用的节点形状.
The example will add a title to the plot. Constructing a legend is more involved but I hope you can proceed further based on this idea. The labels of the legend can be constructed with repeatedly calling the draw
or draw_at
method of a TextDrawer
(after adjusting the text
property of the TextDrawer
between calls of course). You can draw a box around the legend using standard Cairo calls. You can also use the node drawer classes in igraph.drawing.shapes
if you want to draw node shapes similar to those that igraph uses when it draws a graph.
这篇关于将标题和图例添加到igraph图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!