【问题标题】:How to assign different colors to different edges using igraph in Python?如何在 Python 中使用 igraph 为不同的边缘分配不同的颜色?
【发布时间】:2016-08-31 16:56:04
【问题描述】:

我有一个包含三种边的图:“company”、“std”和“res”。许多其他顶点没有边。

当我绘制摘要时,我得到:

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

我想用不同的颜色绘制不同种类的边缘,但我找不到正确的代码。

这是我打印边缘时得到的示例:

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

我用来制作边缘的代码是:

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

这是给我所有边缘“黑色”的代码

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)

提前致谢。

【问题讨论】:

    标签: python plot igraph edges


    【解决方案1】:

    这是一个有效的功能。

    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)
    

    但是,我觉得python有一个小故障。我必须继续重新创建原始图,因为不知何故,即使我更改了图的名称,python 也会保留我添加的边的内存。如果我不重复命令g_groups_all = body.create_groups_edges(g_groups) 边将创建在彼此之上...好像每次我使用 g_groups 添加边时我都在更改 g_groups 本身!算了!

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 2015-04-06
      • 2020-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多