Force nodes positions on concentric circles in graphviz graph

梦想的初衷 提交于 2020-05-24 05:34:46

问题


I am using pygraphviz library to plot a python graph created using networkx library. Overall I am quite happy with the 'neato' layout, which produces something like this:

Now, my data is structured in a way that I always have a central node, then a set of nodes that are at distance 1 from the center, and then another set of nodes wich are at distance 2 from the center. By "distance" I mean number of links before reaching the central node. Because of this, I would like to force the position of the distance-1 nodes on a circumference of a fixed radius from the center, let's say r=1, and the distance-2 nodes on a circumference of r=2. I attach a sketch for clarity:

So far, I tried approaching the problem by manually defining the position of the nodes (this is not a minimum working example, it is just to show my failed approach):

# define distance levels
levels = [0, 1, 2]

# assign a radius to each level
radius = {}
for index, level in enumerate(levels):
    radius[level] = index

# assign nodes to each level
nodes = {}
for level in levels:
    nodes[level] = [x for x,y in graph.nodes(data=True) if y['level']==level]

# calculate position of nodes on the circumferences
pos = {}
for level in levels:
    n_nodes = len(nodes[level])
    # divide pi in n_nodes parts to get the angle increment of each node
    theta = 2*np.pi/n_nodes
    n = 1
    for node in nodes[level]:
        xPos = radius[level]*np.cos(theta*n)
        yPos = radius[level]*np.sin(theta*n)
        pos[node] = (xPos, yPos)
        n += 1

The problem with this method is that the order of the nodes on the circumference is not decided by graphviz anymore, and so the graph actually looks horrible because the arrows are all superimposed and crossing each other: The nice thing of using 'neato' layout is that graphviz decides where to put nodes in order to have, well, a 'neat' graph. So my question is: can I tell to graphviz to keep the nodes on the circles, but still letting it decide what is the best order of the nodes?

来源:https://stackoverflow.com/questions/61425817/force-nodes-positions-on-concentric-circles-in-graphviz-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!