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 drawing commands for networkx accept an argument <code>pos</code>.  </p>

<p>So before creating <code>fig1</code>, define <code>pos</code>  The two lines should be</p>

<pre><code>pos = nx.spring_layout(G) #other layout commands are available.
fig1 = nx.draw_networkx(G, pos = pos)
</code></pre>

<p>later you will do</p>

<pre><code>fig2 = nx.draw_networkx(G, pos=pos).
</code></pre>

python matplotlib networkx