首页 > 解决方案 > 在networkx中重新排列图中的节点和边

问题描述

我使用networkx(Python)的光谱布局绘制了下图。看起来同一张图可以重新排列成一个 3D 立方体,其中的层次关系会更清晰。所有下标“orth”的都可以在同一平面上,即低于所有下标“lex”(它们本身都在一个平面上);这低于所有那些下标的“sem”(它们也占据同一平面)。

我有两个问题:是否有任何绘图布局可以使分层性质变得明显?如果我上面描述的平面结构可以完成这项工作,我该如何实现呢?

代码如下:

import networkx as nx
import matplotlib.pyplot as plt

G = nx.DiGraph()


edges = [('$x_{orth}$', '$e_{orth}$'),\
     ('$r_{orth}$', '$e_{orth}$'),\
     ('$e_{orth}$', '$y_{lex}$'),\
     ('$r_{lex}$', '$x_{orth}$'), \
     ('$y_{lex}$', '$r_{orth}$'), \
     ('$y_{lex}$', '$x_{lex}$'), \
     ('$y_{lex}$', '$y_{lex}$'),\

     ('$x_{lex}$', '$e_{lex}$'),\
     ('$r_{lex}$', '$e_{lex}$'),\
     ('$e_{lex}$', '$y_{sem}$'),\
     ('$r_{sem}$', '$x_{lex}$'), \
     ('$y_{sem}$', '$r_{lex}$'), \
     ('$y_{sem}$', '$x_{sem}$'), \
     ('$y_{sem}$', '$y_{sem}$'), \

     ('$x_{sem}$', '$e_{sem}$'),\
     ('$r_{sem}$', '$e_{sem}$'),\
     ('$e_{sem}$', '$y_{mng}$'),\
     ('$y_{mng}$', '$r_{sem}$'), \
     ('$y_{mng}$', '$y_{mng}$')]

G.add_edges_from(edges)


# separate calls to draw nodes and edges
pos = nx.spectral_layout(G)
nx.draw_networkx_nodes(G, pos,node_size = 500)
nx.draw_networkx_labels(G, pos)
nx.draw_networkx_edges(G, pos, edgelist=edges, edge_color='black', arrows=True)
plt.show()
enter code here

在此处输入图像描述

标签: plotnetworkxgraph-theory

解决方案


推荐阅读