1
votes

This is a silly question, but I just can't see how to do it in the igraph documentation. It will be very simple I imagine, but my python isn't good enough to work it out.

using igraph, I am finding the edge I want to play with using select. It returns a reference to the edge object. When I try to change the edge_width attribute it isn't being updated in the plot.

My code example is looking for the edge between vertices A and 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

But when I plot the graph, all edges are the same. What am I doing wrong?

EDIT: To make life easier, here is a full code example that generates the issue. It simply creates a graph with 5 nodes, A to E, fully connected to each other and plots it to screen.

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
You are using the edge_width attribute in your example; you should be using width instead, as you correctly do in your updated answer. - Tamás

2 Answers

2
votes

I still can't see an easy way to find and change visual properties of a single edge, but I managed with this code snippet (after much trial and error).

# 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

While this is great when you want to update a whole load of edges, it seems ridiculously clunky when I just want to update one or two. Still, it works so what the hey.

0
votes

They are actually 2 ways to set a single edge "width" attribute:

if I understood your question, you want to update this attribute after having created the graph.

You can do this as follows:

# 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

Otherwise, why not specifying it while building the graph ? You just need to add the width keyword arg to the "add_edge" function. Here is your building loop modified this way:

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)

Hope this helps ;)