如何在 python-graph 中使用 ArrowEdgeDrawer 绘制箭头边缘?

How to draw an arrow edge with ArrowEdgeDrawer in python-graph?

我想绘制一个绘图,显示使用 python-graph 将一个图转换为另一个图。所以我需要一个从第一张图指向第二张图的箭头。我正在考虑使用 ArrowEdgeDrawer class,但我不知道如何正确使用它。任何人都可以向我提供创建和使用 ArrowEdgeDrawer 对象的演示,我们将不胜感激。

ArrowEdgeDrawer 并不意味着用于在绘图上绘制任意箭头;顾名思义,它是一个 edge 抽屉。 Graph drawer 类 默认使用 ArrowEdgeDrawer 将无向边绘制为直线,将有向边绘制为带箭头的线,因此 ArrowEdgeDrawer 的 public 方法将两个图顶点作为参数而不是情节上的任意点。以一种非常复杂的方式,您可以 可能 创建一个包含两个不可见顶点(顶点形状 = "none")和它们之间的有向边的 "fake" 图,并且然后将这张图叠加在你实际的图上,但我认为直接在图上画画 canvas 更容易。 igraph 使用 Cairo 作为绘图后端,您可以使用适当构造的 Plot 对象的 surface 属性 访问 igraph 图的 Cairo 表面。然后,您可以为表面创建 Cairo 绘图上下文并直接在其上绘图。例如:

from igraph import Graph, Plot
from cairo import Context

# Create two graphs
g = Graph.Ring(5)
g2 = Graph.Full(5)

# Create a figure containing the two graphs
fig = Plot("test.pdf", bbox=(800, 360), background="white")
fig.add(g, bbox=(20,20,340,340))
fig.add(g2, bbox=(460,20,780,340))

# Force the figure to be drawn
fig.redraw()

# Create a Cairo drawing context
ctx = Context(fig.surface)

# Draw an arrow
ctx.set_source_rgb(0,0,0)
ctx.move_to(360,180)
ctx.line_to(430,180)
ctx.stroke()
ctx.move_to(440,180)
ctx.line_to(430,170)
ctx.line_to(430,190)
ctx.line_to(440,180)
ctx.fill()

# Save the figure
fig.save()