我写了一个代码将我的转换MultiDiGraph()
为pydot
图表以显示自循环和箭头,但转换后,pydot
图表A
失去了所有属性G
。如何更改图形的节点大小A
并将它们设置为等于node_sizes[]
列表中的相应值,就像我所做的那样G
?
代码:
def draw_graph(graph, size):
# extract nodes from graph
nodes = set([n1 for n1, n2 in graph] + [n2 for n1, n2 in graph])
print("Nodes ",nodes,"\n")
node_sizes = []
for n in nodes:
#Scaling up the node importance by a factor of 2000 to make it visible
node_sizes.append( size[n] * 2000)
print("Node size ",node_sizes,"\n")
# create networkx graph
G=nx.MultiDiGraph()
G.add_edges_from(edges)
G.add_nodes_from(nodes)
edge_colours = ['black' for edge in G.edges()]
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos, cmap=plt.get_cmap('jet'), node_size = node_sizes)
nx.draw_networkx_labels(G, pos)
nx.draw_networkx_edges(G, pos, edge_color='black', arrows=True)
plt.show()
# render pydot by calling dot, no file saved to disk
A=nx.to_pydot(G, strict=True)
png_str = A.create_png(prog='dot')
# treat the dot output string as an image file
sio = BytesIO()
sio.write(png_str)
sio.seek(0)
img = mpimg.imread(sio)
# plot the image
imgplot = plt.imshow(img, aspect='equal')
plt.show(block=False)