我可以在IGraph-Python中更改包含特定顶点的边缘的颜色吗 [英] Can i change the colour of edges containing specific vertices in IGraph-Python

查看:128
本文介绍了我可以在IGraph-Python中更改包含特定顶点的边缘的颜色吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个有向图,并通过一个程序找出了它包含的所有循环.创建图形后,我想更改包含循环中涉及的顶点的边缘的颜色.

I have created a directed graph and through a program i am finding out all the cycles it contains. After creating the graph i want to change the colour of the edges which contain the vertices involved in the cycle.

我正在使用python igraph.

I am using python igraph.

请帮助

推荐答案

类似以下内容:

vertex_set = set(vertices_in_cycle)
g.es["color"] = "black"
red_edges = g.es.select(_source_in=vertex_set, _target_in=vertex_set)
red_edges["color"] = "red"

说明:

  1. g.es表示图中所有边的集合. (类似地,g.vs是所有顶点的集合.)

  1. g.es represents the set of all edges in the graph. (Similarly, g.vs is the set of all vertices).

g.es["color"]可让您为图形中所有边缘的 all color属性分配一个值.绘图仪使用此边缘属性来确定边缘应具有的颜色.因此,在第2行中,将所有边缘的颜色设置为黑色. (注意:您也可以在此处使用列表,而不是简单的字符串,也可以将HTML颜色符号用于自定义颜色.)

g.es["color"] lets you assign a value to the color attribute of all the edges in the graph. This edge attribute is used by the plotter to decide what color an edge should have. Therefore, in line 2, you are setting the color of all the edges to black. (Note: you could also use a list here instead of a simple string, or you could use HTML color notations for custom colors).

您可以使用g.es作为列表,在这种情况下,您可以获得图形的特定边缘;例如g.es[2]会为您提供id = 2的优势.这里不使用它,但是很高兴知道.

You could use g.es as a list, in which case you get a particular edge of the graph; e.g., g.es[2] would give you the edge with id=2. This is not used here, but it's good to know.

g.es.select是一种基于某些条件选择边缘子集的方法. help(EdgeSeq.select)为您提供了更多有关此的信息;这里的要点是,在第3行中,您正在选择两个端点都位于感兴趣的顶点集中的所有边.选定的边存储在red_edges变量中,该变量的类型与g.es (即EdgeSeq).

g.es.select is a method that selects a subset of the edges based on some criteria. help(EdgeSeq.select) gives you more info about this; the point here is that in line 3, you are selecting all the edges for which both endpoints lie in the vertex set you are interested in. The selected edges are stored in the red_edges variable, which has the same type as g.es (i.e. EdgeSeq).

在最后一行中,您将red_edges中所有边缘的颜色设置为red,覆盖了您在第2行中设置的黑色.

In the last row, you are setting the color of all the edges in red_edges to red, overriding the black color you have set in line 2.

请注意,上面的代码不仅会将循环的边缘涂成红色,还将循环的所有和弦涂成红色.

Note that the above code will paint not only the edges of the cycle to red but also all the chords of the cycle.

更新:如果由于某些原因上述代码中的第3行无法正常工作,则可以将第2行和第3行替换为以下内容:

Update: if line 3 in the above code does not work for you for some reason, you can replace lines 2 and 3 with the following:

g.es["color"] = ["red" if (edge.source in vertex_set and \
                           edge.target in vertex_set) else "black" \
                 for edge in g.es]

这篇关于我可以在IGraph-Python中更改包含特定顶点的边缘的颜色吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆