[英]NetworkX: plotting the same graph first intact and then with a few nodes removed
假设我有一个包含10
节点的图形,并且在以下情况下想绘制它:
如何确定第二个图与第一个图完全相同?
我的尝试将生成两个以不同布局绘制的图形:
import networkx as nx
import matplotlib.pyplot as plt
%pylab inline
#Intact
G=nx.barabasi_albert_graph(10,3)
fig1=nx.draw_networkx(G)
#Two nodes are removed
e=[4,6]
G.remove_nodes_from(e)
plt.figure()
fig2=nx.draw_networkx(G)
networkx的绘图命令接受参数pos
。
因此,在创建fig1
之前,请定义pos
。
pos = nx.spring_layout(G) #other layout commands are available.
fig1 = nx.draw_networkx(G, pos = pos)
以后你会做
fig2 = nx.draw_networkx(G, pos=pos).
以下对我有用:
import networkx as nx
import matplotlib.pyplot as plt
from random import random
figure = plt.figure()
#Intact
G=nx.barabasi_albert_graph(10,3)
node_pose = {}
for i in G.nodes_iter():
node_pose[i] = (random(),random())
plt.subplot(121)
fig1 = nx.draw_networkx(G,pos=node_pose, fixed=node_pose.keys())
#Two nodes are removed
e=[4,6]
G.remove_nodes_from(e)
plt.subplot(122)
fig2 = nx.draw_networkx(G,pos=node_pose, fixed=node_pose.keys())
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.