I wrote a code to convert my MultiDiGraph()
to a pydot
graph to display self loops and arrows but after conversion, the pydot
graph A
has lost all the attributes of G
. How can change the node size of graph A
and set them equal to corresponding value in node_sizes[]
list like I did for G
?
code:
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)