简体   繁体   中英

Draw Two Graph in Different Positions in NetworkX

I Want to Draw two Different Graph in Different Positions Using Networkx, Eg G1 on left side of canvas and G2 on the right side ,But I dont know how to do it, here is my code:

import networkx as nx
import matplotlib.pyplot as plt

import socialModels as sm
G1 = sm.nearestNeighbor_mod(256, 0.6, 1)
G2 = sm.nearestNeighbor_mod(256, 0.6, 1)
elarge1 =[(u,v) for (u,v,d) in G1.edges(data=True)]
elarge2 =[(u,v) for (u,v,d) in G2.edges(data=True)]

pos1=nx.spring_layout(G1)
pos2=nx.spring_layout(G2)
nx.draw_networkx_nodes(G1,pos1,node_size=30,node_color='b')
nx.draw_networkx_edges(G1,pos1,edgelist=elarge1,width=1,style='solid')


nx.draw_networkx_nodes(G2,pos2,node_size=50)
nx.draw_networkx_edges(G2,pos2,edgelist=elarge2,width=1)


nx.write_graphml(G1,'test.graphml')
plt.show() # display
plt.draw();

How about shifting the positions in pos2 with the desired value?

A working example would be

import networkx as nx
import matplotlib.pyplot as plt

G1 = nx.balanced_tree(2,1)
G2 = nx.balanced_tree(2,2)


elarge1 =[(u,v) for (u,v,d) in G1.edges(data=True)]
elarge2 =[(u,v) for (u,v,d) in G2.edges(data=True)]


pos1=nx.spring_layout(G1)
pos2=nx.spring_layout(G2)


for k,v in pos2.items():
    # Shift the x values of every node by 10 to the right
    v[0] = v[0] +10

nx.draw_networkx_nodes(G1,pos1,node_size=30,node_color='b')
nx.draw_networkx_edges(G1,pos1,edgelist=elarge1,width=1,style='solid')


nx.draw_networkx_nodes(G2,pos2,node_size=50)
nx.draw_networkx_edges(G2,pos2,edgelist=elarge2,width=1)

plt.show() # display
plt.draw()

You can think about a good value of shifting by evaluating the positions in Graph one or two and compute your desired shift...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM