我已经创建了一个图表,如下所示。但是,我想按距离头部最短的距离(A)分层排列图形。换句话说:C、B、D、E都应该在同一水平面上,并且水平对齐,因为它们都离A有一条边(最短路径)。然后,F、G、H应该在下一水平面上,因为它们都离A有两条边,以此类推
我喜欢图形的外观,因此解决方案最好保持这种可视化风格
import matplotlib.pyplot as plt
import networkx as nx
import pydot
from networkx.drawing.nx_pydot import graphviz_layout
from IPython.display import Image, display
G=nx.Graph()
G.add_edges_from([
('A','B'),
('A','C'),
('A','E'),
('A','D'),
('B','C'),
('B','F'),
('C','F'),
('D','H'),
('D','G'),
('E','H'),
('F','I'),
('G','I'),
('G','J'),
('H','J'),
('I','K'),
('J','K')
])
pdot = nx.drawing.nx_pydot.to_pydot(G)
graph = Image(pdot.create_png())
display(graph)
有一个内置函数,允许计算到给定节点的最短路径的长度。长度可用于指定一个坐标。 另一个坐标是根据与
A
距离相同的节点数计算的:相关问题 更多 >
编程相关推荐