NetworkX: plotting the same graph first intact and then with a few nodes removed
<p>Say I have a graph with <code>10</code> nodes, and I want to plot it when:</p>

<ol>
<li>It is intact</li>
<li>It has had a couple of nodes removed</li>
</ol>

<p><strong>How can I make sure that the second plot has exactly the same positions as the first one?</strong></p>

<p>My attempt generates two graphs that are drawn with a different layout:</p>

<pre><code>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)
</code></pre>

<p>The following works for me:</p>

<pre><code>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()
</code></pre>

<p><a href="http://i.stack.imgur.com/FHRXc.png" rel="nofollow"><img src="http://i.stack.imgur.com/FHRXc.png" alt="enter image description here"></a></p>

python matplotlib networkx