【问题标题】:Vertices coordinates in graph-tool图形工具中的顶点坐标
【发布时间】:2016-06-26 21:08:58
【问题描述】:

我想以一种有效的方式在 graph-tool 中指定一个图的顶点坐标。

给定一个看起来像这样的 csv:

Node,X,Y

1,2.5,3.8

2,3.4,2.9

...

我希望图形工具在 (2.5,3.8) 等位置绘制顶点 1...

一个无效的解决方案如下: Explicit vertex position in python graph-tool ,所以我基本上可以在我的所有坐标上使用 for 循环并将它们保存在属性映射“pos”中。如果我的图表是“g”并且我的 csv 是使用数据框“坐标”中的 pandas 读取的,我可以这样做:

for i in range(1,numnodes+1):
    pos[g.vertex(i)] = (coordinates.values[i-1,1],coordinates.values[i-1,2]) 

问题是我的节点数 numnodes 很大(~10^7),这可能需要一些时间。

有没有更有效的方法来做这个操作,直接在属性映射'pos'中输入数据?

【问题讨论】:

  • 您可以尝试以矢量化方式使用它:pos = coordinates[['X','Y']].values 而不是循环吗?我不知道graph-tool 模块,但我想它应该能够与 numpy 数组一起使用,甚至可能与 pandas 数据框一起使用......

标签: python performance pandas graph graph-tool


【解决方案1】:

我找到了我的问题的答案,一个有效的方法是使用 .set_2d_array() 函数;

pos.set_2d_array(coordinates[['X','Y']].values.T)

成功了。 这里的“.T”是转置函数,是numpy库的一部分。

【讨论】:

    【解决方案2】:

    我会试试这个:

    pos = coordinates[['X','Y']].values
    

    如果 graph-tool 接受 numpy 数组,否则:

    pos = [tuple(t) for t in coordinates[['X','Y']].values]
    

    【讨论】:

    • 感谢您的回答!不幸的是,两者都不起作用,我怀疑这是因为 'pos' 在执行此操作时丢失了它的类型。例如,如果我 for e in pos: print e 我得到 array([ 2.5, 3.8]) array([ 3.4, 2.9]) ...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    • 2011-03-27
    • 1970-01-01
    相关资源
    最近更新 更多