Position/showing of labels with networkx + graphviz

爷,独闯天下 提交于 2019-12-01 10:46:04

Ok, so I've partially solved the second question: how to plot from the aggregation nodes onwards. To do so I estimate the number of hops towards the latest one. After that I decide to label the nodes below the threshold.

def getHopToNH(nodes):
    path        = []
    labelList   = {}

    for startNode in G.nodes():
        endNode = 'myLabel'
        try:
            p = len(nx.shortest_path(G,source=startNode,target=endNode))
        except:
            p = -1
        path.append((startNode,p))

        if p < 8:
            labelList = {**labelList,**{str(startNode):str(startNode)}}
        else:
            labelList = {**labelList,**{str(startNode):''}}

    return labelList

UPDATE:

Now, in order to re-position the labels, I had to modify the position myself.

for p in pos:

    yOffSet = -300
    xOffSet = -400

    pos[p] = (pos[p][0]+xOffSet,pos[p][1]+yOffSet)

labelDescr = nx.draw_networkx_labels(G,
    pos         = pos,
    labels      = nodeLabelDict,
    font_size   = nodeFontSize,)

for n,t in labelDescr.items():
    finDegree = 70
    t.set_rotation(finDegree)

After this, I get to plot the following:

And I really like this output now ... :-)

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