【问题标题】:igraph: How to fix node position when drawing incrementally growing subgraphs of the same graphigraph:绘制同一图的增量增长子图时如何修复节点位置
【发布时间】:2015-10-27 08:19:21
【问题描述】:

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

但是随着节点不断被添加到图表中,之前的节点不一定保持其旧位置,类似于in this post 中描述的内容

我的代码如下:

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,
          )

以下是两个连续的快照。

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

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

【问题讨论】:

    标签: python igraph graph-drawing


    【解决方案1】:

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

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

    【讨论】:

    • 非常感谢塔马斯!我应该想到的,好把戏!另请注意另一种制作人工节点而不显示的方法是将size属性设置为0.0
    猜你喜欢
    • 2022-01-21
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    • 1970-01-01
    • 2014-08-09
    • 1970-01-01
    • 1970-01-01
    • 2012-12-22
    相关资源
    最近更新 更多