在Python graph-tool中显式设置顶点位置

6 投票
1 回答
3211 浏览
提问于 2025-04-17 03:59

我正在使用 Python 的 graph-tool 库来绘制图形。为了画图,它使用一个叫做 graph_draw 函数。我想把每个点的位置明确地传给 dot 引擎。结果发现,我可以传一个叫做 pos 的属性图。于是我尝试定义它为 v_pos = g.new_vertex_property("vector<double>"),其中 g 是我的图。我不确定这样做是否正确。

这里有一段代码,你可能会觉得有用。

pos = gt.random_layout(g, shape=shape, dim=3)
>>> pos[g.vertex(0)].a
array([ 86.59969709,   1.31435598,   0.64651486])
graph_draw(g, pos=pos, output="graph-draw-random.pdf")

如果我想把我的点的位置定义为 (0,2)、(0,4)…(0,8),我该怎么做呢?

在上面的代码中,我可以把维度改成 2。但我不想要随机布局。

作为参考,这里是我使用的这个工具的主页。 http://projects.skewed.de/graph-tool/

1 个回答

6

你可以很简单地设置位置,方法如下:

   pos = g.new_vertex_property("vector<double>")
   pos[g.vertex(0)] = (0, 2)
   pos[g.vertex(1)] = (0, 4)
   ...

撰写回答