是否可以从NetworkX布局中可视化图形,如“三月疯狂”括号?

2024-09-30 01:27:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将python中的一个锦标赛定向图可视化为networkx对象。理想的情况下,如果我能想象它类似于经典的NCAA“三月疯狂”等级,那就太好了。到目前为止,最接近我的,是使用点布局,但它不是很直观,看起来一点也不像括号。在

我有一个单独的图形,但可以把它分成4个子图(3月疯狂的设置方式),如果这能使可视化更好。在

下面是我要找的布局的一个示例(或附近的布局) NCAA "March Madness" bracket
(来源:shorebranding.com

这是我到目前为止得到的结果,它需要改进,但是你会发现这是一个糟糕的图形表示。 img

下面是我现在用来构建和输出图形到图像的代码:

def buildTourneyGraph(games,analysisYear):
    MG=nx.DiGraph()

    #for  idx, gm in enumerate(games):    # Iterate through rows
    for  gm in games:    # Iterate through rows
        #pp.pprint(gm)
        if gm["academicYear".upper()] == int(analysisYear):

            #add the two team nodes
            MG.add_node(gm["winner".upper()])
            MG.add_node(gm["loser".upper()])
            MG.add_edge(gm["loser".upper()], gm["winner".upper()], weight=gm["margin".upper()] , round=gm["round".upper()])

    #Draw a graph and save it to a PNG file
    #nx.draw_spectral(MG)

    #nx.draw_graphviz(MG)
    #nx.draw_shell(MG)

    # same layout using matplotlib with labels
    plt.title("NCAA Tourney for " + str(analysisYear))
    pos=nx.graphviz_layout(MG,prog='dot')
    nx.draw(MG,pos,with_labels=True,arrows=True,node_size=20,node_color="red")



    outputGraphFile = os.path.expanduser('C:/Users/myUser/Documents/graph_tourney_' + str(analysisYear) + '.png')
    plt.savefig(outputGraphFile)    

Tags: inaddnode图形for可视化布局upper

热门问题