Python 如何使用 igraph 为不同的边分配不同的颜色?
How to assign different colors to different edges using igraph in Python?
我有一个包含三种边的图:'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
这是给我所有优势的代码'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)
提前致谢。
这是一个有效的函数。
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)
我有一个包含三种边的图:'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
这是给我所有优势的代码'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)
提前致谢。
这是一个有效的函数。
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)