在 graph_tool 中添加边权重和缩放绘制的边长度
Adding edge weights and scaling drawn edge lengths in graph_tool
我正在使用 graph-tool,但找不到定义边权重的方法。如何向我的图形添加边权重?
此外,我希望当我使用 graph_draw
时,图表将根据权重绘制边距。我怎样才能做到这一点?
您正在寻找 Property Maps。来自文档:
Property maps are a way of associating additional information to the vertices, edges or to the graph itself. There are thus three types of property maps: vertex, edge and graph. All of them are handled by the same class, PropertyMap.
属性 映射比简单的边权重灵活得多,因为您可以将任意对象附加到图节点和边上,但是使用它们将边映射到浮点或整数值可以达到相同的效果。
文档中的示例代码:
from itertools import izip
from numpy.random import randint
g = Graph()
g.add_vertex(100)
# insert some random links
for s,t in izip(randint(0, 100, 100), randint(0, 100, 100)):
g.add_edge(g.vertex(s), g.vertex(t))
vprop_double = g.new_vertex_property("double") # Double-precision floating point
vprop_double[g.vertex(10)] = 3.1416
vprop_vint = g.new_vertex_property("vector<int>") # Vector of ints
vprop_vint[g.vertex(40)] = [1, 3, 42, 54]
eprop_dict = g.new_edge_property("object") # Arbitrary python object.
eprop_dict[g.edges().next()] = {"foo": "bar", "gnu": 42} # In this case, a dict.
gprop_bool = g.new_edge_property("bool") # Boolean
gprop_bool[g] = True
对于问题的第二部分,这里是图形工具 drawing and layout documentation,其中包含一些不同的算法,您可以使用这些算法代替 graph_draw
来显示图形。查看接受边 属性 映射作为输入参数的算法。我以前没有使用过它们,但看起来在创建布局对象时传递正确的边缘权重 属性 映射应该会为你处理边缘长度缩放。
我正在使用 graph-tool,但找不到定义边权重的方法。如何向我的图形添加边权重?
此外,我希望当我使用 graph_draw
时,图表将根据权重绘制边距。我怎样才能做到这一点?
您正在寻找 Property Maps。来自文档:
Property maps are a way of associating additional information to the vertices, edges or to the graph itself. There are thus three types of property maps: vertex, edge and graph. All of them are handled by the same class, PropertyMap.
属性 映射比简单的边权重灵活得多,因为您可以将任意对象附加到图节点和边上,但是使用它们将边映射到浮点或整数值可以达到相同的效果。
文档中的示例代码:
from itertools import izip
from numpy.random import randint
g = Graph()
g.add_vertex(100)
# insert some random links
for s,t in izip(randint(0, 100, 100), randint(0, 100, 100)):
g.add_edge(g.vertex(s), g.vertex(t))
vprop_double = g.new_vertex_property("double") # Double-precision floating point
vprop_double[g.vertex(10)] = 3.1416
vprop_vint = g.new_vertex_property("vector<int>") # Vector of ints
vprop_vint[g.vertex(40)] = [1, 3, 42, 54]
eprop_dict = g.new_edge_property("object") # Arbitrary python object.
eprop_dict[g.edges().next()] = {"foo": "bar", "gnu": 42} # In this case, a dict.
gprop_bool = g.new_edge_property("bool") # Boolean
gprop_bool[g] = True
对于问题的第二部分,这里是图形工具 drawing and layout documentation,其中包含一些不同的算法,您可以使用这些算法代替 graph_draw
来显示图形。查看接受边 属性 映射作为输入参数的算法。我以前没有使用过它们,但看起来在创建布局对象时传递正确的边缘权重 属性 映射应该会为你处理边缘长度缩放。