3

我有一个图表,我之前通过在 Gephi 中运行 ForceAtlas2 预先计算了节点的位置,然后我尝试ìgraph在 python 中绘制。每个节点都有一个date属性,所以我遍历所有年份,计算相应的子图并绘制它们以创建动画效果。

然而,随着节点不断被添加到图中,先前的节点不一定保持其旧位置,类似于本文中描述的内容

我的代码如下:

colors = [color_list[int(g.vs[v]["Modularity Class"])] for v in range(0, len(g.vs))]
label_sizes = [sz / 4.0 for sz in g.vs["size"]]

nodes_in_years = defaultdict(list) # keeps a list of nodes per year
for v in g.vs:
    year = int(v["date"][:4]) # convert the first 4 digits of the date to year
    nodes_in_years[year].append(v)

nodes_incremental = []
for y, nodes in nodes_in_years.items():
    nodes_incremental += nodes
    h = g.subgraph(nodes_incremental)

    plot(h, "graph-igraph%d.png"%y,
         bbox=(640,480),
         vertex_color=colors,
         vertex_label_size=label_sizes,
         keep_aspect_ratio=False,
          )

以下是两个连续的快照。 图表 2001 图表 2002

在第二个快照中,随着更多节点被添加到左侧,节点被略微“挤压”。

如何将节点保持在固定位置?我尝试了xlim设置,ylim但我可能没有正确设置值

4

1 回答 1

4

xlimylim用于igraph的R接口;它们在 Python 中无效。通常使用的技巧是在所有快照中找到最小和最大 X 和 Y 坐标,然后在布局的左上角和右下角放置两个假顶点(它们的shape属性设置为)。None确保在所有具有相同坐标的快照中包含这些假顶点 - 这将确保布局在每个快照中以完全相同的方式缩放以适合您指定的边界框。

如果您没有shape在当前布局中使用该属性,请注意将shape属性添加到假顶点也会将shape属性(默认值为None)添加到“真实”顶点,因此您应该手动设置shape所有顶点的属性除了这两个假的"circle"

于 2015-10-27T00:19:21.507 回答