Kha*_*ilz 6 python math tree igraph nodes
使用Radial Positions algorithm从这个问题转换为 python 的 C ,我已经成功地创建了一个基于根节点的径向图,一直到每个节点的最后一个子节点。现在我有一个新问题,我有多个根节点,需要它们以找到的第一个根为中心,或者至少是一个中心点。
我发现的最接近的例子是这张图:
到目前为止,我能想到的就是对于找到的每个根节点,将其乘以它的索引,然后将 x 位置与 y 半径相加。到目前为止,它的效果还不是很好,因为我的子节点没有遵循它。我已经被这个问题难住了几天了。
def RadialPositions(node, id):
children_in_node = len(timelinedatta.neighbors(id, mode="out"))
def rotate_node(x, y, nangle):
nx = x * math.cos(nangle) - y * math.sin(nangle)
ny = x * math.sin(nangle) + y * math.cos(nangle)
return nx, ny
def get_depth(id):
count = 0
for v in timelinedatta.bfsiter(id, mode="in", advanced=True):
count = count + 1
return count - 1
if len(timelinedatta.neighbors(id, mode="out")) > 0 and len(timelinedatta.neighbors(id, mode="in")) == 0:
node["positions"] = (0, 0)
node["depth"] = get_depth(id)
node_children_list = []
for child in timelinedatta.neighbors(id, mode="out"):
node_children_list.append((child, timelinedatta.vs[child]))
for idx, (child_node_id, child_node) in enumerate(node_children_list, start=0):
centerAdjust = 0
if timelinedatta.neighbors(id, mode="in"):
centerAdjust = (-node["angleRange"] + node["angleRange"] / children_in_node) / 2
child_node["depth"] = get_depth(child_node_id)
child_depth = child_node["depth"]
child_node["nodeAngle"] = node["nodeAngle"] + node["angleRange"] / children_in_node * idx + centerAdjust
child_node["angleRange"] = node["angleRange"] / children_in_node
nx = rotate_node(40 * child_depth, 0, child_node["nodeAngle"])[0]
ny = rotate_node(40 * child_depth, 0, child_node["nodeAngle"])[1]
child_node["positions"] = [2 * nx, 2 * ny]
RadialPositions(child_node, child_node_id)
Run Code Online (Sandbox Code Playgroud)
我有我的图形示例,这里对引擎收录,以及
您可以将graphviz(按照 @thoku 的建议)与neato布局引擎 ( man neato) 一起使用:
neato使用弹簧模型并减少相关能量来绘制无向图(参见Kamada 和 Kawai,InformationProcessingLetters 31:1,1989 年 4 月)。
例如,这个例子
from graphviz import Digraph
tree = Digraph(engine='neato')
tree.node('root1',"R1")
tree.node('root2',"R2")
tree.node('root3',"R3")
tree.node('root4',"R4")
tree.edge('root1','root2')
tree.edge('root1','root3')
tree.edge('root3','root4')
tree.node('child11',"C11")
tree.node('child12',"C12")
tree.node('child13',"C13")
tree.edge('root1','child11')
tree.edge('root1','child12')
tree.edge('root1','child13')
tree.node('child21',"C21")
tree.node('child22',"C22")
tree.edge('root2','child21')
tree.edge('root2','child22')
tree.node('child31',"C31")
tree.node('child32',"C32")
tree.edge('root3','child31')
tree.edge('root3','child32')
tree.node('child41',"C41")
tree.node('child42',"C42")
tree.node('child43',"C43")
tree.edge('root4','child41')
tree.edge('root4','child42')
tree.edge('root4','child43')
tree.render("tree")
Run Code Online (Sandbox Code Playgroud)
生成以下输出: