igraph通过EID python设置边缘宽度

时间:2014-05-20 15:00:13

标签: python igraph

这是一个愚蠢的问题,但我无法在igraph文档中看到如何做到这一点。我想,这将非常简单,但我的python还不够好。

使用igraph,我发现使用select我想玩的边缘。它返回对边缘对象的引用。当我尝试更改edge_width属性时,它不会在图中更新。

我的代码示例正在寻找顶点A和B之间的边缘。

 source = g.vs.find(name = 'A')
 sink   = g.vs.find(name = 'B')
 edge   = g.es.select(_source = source, _target= sink)
 edge["edge_width"] = 20

但是当我绘制图形时,所有边都是相同的。我做错了什么?

编辑:为了让生活更轻松,这里有一个生成问题的完整代码示例。它只是创建一个包含5个节点A到E的图形,它们彼此完全连接并将其绘制到屏幕上。

import string
import igraph as ig

num_nodes = 5

alpha_list = list(string.ascii_uppercase)
alpha_list = alpha_list[:num_nodes]

g = ig.Graph()
g.add_vertices(alpha_list)

for x in range (0, num_nodes + 1):
    for y in range (x, num_nodes):
        print "x: "+str(x)+", y: "+str(y)
        if (x != y):
            g.add_edge(x, y)

g.vs["label"] = g.vs["name"]

source = g.vs.find(name = 'A')
sink   = g.vs.find(name = 'B')

edge = g.es.select(_source = source, _target= sink)

edge["edge_width"] = 20

print edge.attributes()

layout = g.layout("circle")
ig.plot(g, layout = layout)

2 个答案:

答案 0 :(得分:2)

我仍然看不到一种简单的方法来查找和更改单个边缘的视觉属性,但我使用此代码片段进行了管理(经过多次试验和错误)。

# Start a list of all edge widths, defaulted to width of 3
widths = [3] * len(g.es)

# Find edge ID
start_vertex = g.vs.find(name = start_name).index
end_vertex   = g.vs.find(name = end_name).index
edge_index   = g.get_eid(start_vertex, end_vertex)

# Change the width for the edge required
widths[edge_index] = 20

# Update the graph with the list of widths
g.es['width'] = widths

虽然当你想要更新整个边缘时这很好,但当我只想更新一两个时,它看起来非常笨拙。不过,它仍然有效。嘿。

答案 1 :(得分:0)

它们实际上是设置单个边缘的两种方式"宽度"属性:

如果我理解了您的问题,您希望在创建图表后更新此属性

您可以按照以下方式执行此操作:

# get index of edge between vertices "A" and "B"
edge_index = g.get_eis("A", "B")
# set width attribute to 20
g.es[edge_index] = 20

否则,为什么不在构建图形时指定? 您只需要将宽度关键字arg添加到" add_edge"功能。这是以这种方式修改的构建循环:

for x in range (0, num_nodes + 1):
    for y in range (x, num_nodes):
        print "x: "+str(x)+", y: "+str(y)
        if (x != y):
            # setting width 20 for "A" and "B"
            w = 20 if (x==0 and y==1) else 1
            g.add_edge(x, y, width=w)

希望这会有所帮助;)