旧答案中的代码无法运行,因为add_path() 的语法在写这篇文章后发生了变化。我编辑了旧问题中的答案,但由于我还没有编辑批准权限,因此不会立即显示。
如果你替换
G.add_path([0,2,5])
G.add_path([1,4,3,0])
G.add_path([2,4,0,5])
与
nx.add_path(G, [0,2,5])
nx.add_path(G, [1,4,3,0])
nx.add_path(G, [2,4,0,5])
那么我相信它应该会成功运行。
编辑:针对下面的评论,这里是一个考虑到形状和大小的工作代码示例。它的风格不是特别干净或一致,但它结合了previous SO question 中的方法和提问者的数据生成方案。
关键部分是从使用nx.draw() 更改为使用nx.draw_networkx_nodes()、nx.draw_networkx_edges() 和nx.draw_networkx_labels() 分别绘制图形的所有部分。有关详细信息,请参阅networkx drawing docs。此更改允许使用 nx.draw_networkx_nodes() 的不同调用来绘制具有不同形状的每组节点。
我做了一些相当不雅的事情来调整情节,包括调整plt.xlim、plt.ylim和nx.layout.spring_layout()的间距参数(k)。
下面的代码给出了如下图:
class Vert:
# default constructor
def __init__(self, name, size, edges):
self.name = name
self.size = size
self.edges = edges
import networkx as nx
import matplotlib.pyplot as plt
nodes = []
nodes.append(Vert('A', 1, ['B', 'C']))
nodes.append(Vert('B', 3, ['D']))
nodes.append(Vert('C', 4, ['D']))
nodes.append(Vert('D', 7, []))
nodes.append(Vert('Y', 64, []))
G = nx.DiGraph()
for v in nodes:
# Assign 'v' shape to even nodes and square shape to odd nodes.
if ord(v.name) % 2 == 0:
G.add_node(v.name, size=v.size, shape='v')
else:
G.add_node(v.name, size=v.size, shape='s')
for e in v.edges:
G.add_edge(v.name, e)
shapes = set((aShape[1]['shape'] for aShape in G.nodes(data = True)))
pos = nx.layout.spring_layout(G, k=2) #Make k larger to space out nodes more.
for shape in shapes:
nodelist=[node[0] for node in filter(lambda x: x[1]['shape']==shape,G.nodes(data = True))]
sizes = [100 * node[1]['size'] for node in filter(lambda x: x[1]['shape']==shape,G.nodes(data = True))]
#...filter and draw the subset of nodes with the same symbol in the positions that are now known through the use of the layout.
nx.draw_networkx_nodes(G,
pos,
node_shape=shape,
nodelist=nodelist,
node_size=sizes)
# Draw the edges between the nodes and label them
nx.draw_networkx_edges(G,pos)
nx.draw_networkx_labels(G, pos)
plt.xlim(-2, 2) # Expand limits if large nodes spill over plot.
plt.ylim(-2, 2) # Expand limits if large nodes spill over plot.
plt.show()