Search code examples
pythonplotigraphedges

How to assign different colors to different edges using igraph in Python?


I have a graph with three kinds of edges: 'company', 'std' and 'res'. Many other vertices have no edges.

When I plot a summary, I get:

IGRAPH UN-- 500 36 -- 
+ attr: area (v), cnpj (v), grande_area (v), name (v), res (v), std (v), company (e), res (e), std (e)

I want to plot the different kinds of edges in different colors, but I cannot find the right code.

Here is a sample of what I get when I print the edges:

igraph.Edge(<igraph.Graph object at 0x00000000045A4B88>, 7, {'res': True, 'company': None, 'std': None})
igraph.Edge(<igraph.Graph object at 0x00000000045A4B88>, 8, {'res': True, 'company': None, 'std': None})
igraph.Edge(<igraph.Graph object at 0x00000000045A4B88>, 9, {'res': True, 'company': None, 'std': None})
igraph.Edge(<igraph.Graph object at 0x00000000045A4B88>, 10, {'res': None, 'company': True, 'std': None})
igraph.Edge(<igraph.Graph object at 0x00000000045A4B88>, 11, {'res': True, 'company': None, 'std': None})
igraph.Edge(<igraph.Graph object at 0x00000000045A4B88>, 12, {'res': None, 'company': True, 'std': None})

The code I used to make the edges is:

def edge_group(g):
    for v in g.vs:
        for w in g.vs:
            if v != w:
                if g.are_connected(v, w) is False:
                    if v['name'].get_cnpj() != "":
                        if v["name"].get_cnpj() == w["name"].get_cnpj():
                            g.add_edge(v, w, company=True)
                    if len(set(v['std']).intersection(w['std'])) > 0:
                        g.add_edge(v, w, std=True)
                    if len(set(v['res']).intersection(w['res'])) > 0:
                        g.add_edge(v, w, res=True)
    return g

And here is the code that is giving me all edges 'black'

def drawing_group(g):
    layout = g.layout("fr")
    visual_style = {}
    visual_style["vertex_size"] = 1
    if g.es["company"] is True and g.es['res'] is True and g.es['std'] is True:
        visual_style['edge_color'] = 'black'
    if g.es["company"] is True and g.es['res'] is True:
        visual_style['edge_color'] = 'grey'
    if g.es["company"] is True and g.es['std'] is True:
        visual_style['edge_color'] = 'green'
    if g.es['company'] is True:
        visual_style['edge_color'] = 'blue'
    if g.es['res'] is True and g.es['std'] is True:
        visual_style['edge_color'] = 'red'
    if g.es['res'] is True:
        visual_style['edge_color'] = 'yellow'
    if g.es['std'] is True:
        visual_style['edge_color'] = 'brown'
    visual_style["layout"] = layout
    visual_style["bbox"] = (500, 500)
    visual_style["margin"] = 20
    visual_style['hovermode'] = 'closest'

    igraph.plot(g, 'output/gr_%s.png' % num, **visual_style)

Thanks in advance.


Solution

  • Here is a function that does work.

    def drawing_group(g, name='a'):
        layout = g.layout("fr")
        visual_style = {}
        for e in g.es:
            if e['company'] is True:
                e['color'] = 'blue'
            elif e['res'] is True:
                e['color'] = 'red'
            elif e['std'] is True:
                e['color'] = 'green'
        visual_style["vertex_size"] = 1
        visual_style["layout"] = layout
        visual_style["bbox"] = (500, 500)
        visual_style["margin"] = 20
        visual_style['hovermode'] = 'closest'
        igraph.plot(g, 'output/gr_%s.png' % (str(num) + name), **visual_style)
    

    However, I feel there is a glitch in python. I have to keep recreating the original graph because, somehow, python is keeping the memory of the edges I add even if I change the name of the graph. If I don't repeat the command g_groups_all = body.create_groups_edges(g_groups) the edges are created on top of each other... As if everytime I use g_groups to add edges I am changing g_groups itself! Go figure!

    g_student = body.create_groups_edges_std(g_groups)
    g_groups = body.create_vertex_groups(my_groups)
    g_researcher = body.create_groups_edges_res(g_groups)
    g_groups = body.create_vertex_groups(my_groups)
    g_company = body.create_groups_edges_company(g_groups)
    g_groups = body.create_vertex_groups(my_groups)
    g_groups_all = body.create_groups_edges(g_groups)