Matplotpib figure in a loop not responding
<p>Python 2.7.11, Win 7, x64, Numpy 1.10.4, matplotlib 1.5.1</p>

<p>I ran the following script from an iPython console after entering <code>%matplotlib qt</code> at the command line</p>

<pre><code>from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import axes3d
import numpy as np

number = input("Number: ")
coords = np.array(np.random.randint(0, number, (number, 3)))

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(coords[:,0], coords[:,1], coords[:,2])
plt.show()
</code></pre>

<p>It plots a random scatter in 3D. So I thought it would be a trivial matter to just pop it into a while loop &amp; get a new figure on each iteration.</p>

<pre><code>from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import axes3d
import numpy as np

s = True
while s:

    number = input("Number: ")
    coords = np.array(np.random.randint(0, number, (number, 3)))

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.scatter(coords[:,0], coords[:,1], coords[:,2])
    plt.show()
    cont = input("Continue? (y/n)")
    if cont == 'n':
        s = False
</code></pre>

<p>...but the figures are just blank &amp; unresponsive until I enter an input for <code>cont</code> then I get,</p>

<blockquote>
  <p>NameError: name 'y' is not defined</p>
</blockquote>

<p>...and the whole thing crashes.</p>

<p>So what am I missing here?</p>

<p>EDIT: Taking into account Aquatically challenged's answer below. The figures still hang until the loop is exited, then they are all plotted at the same time. Anybody know why the plots are not done within the loop?</p>

<p><a href="https://docs.python.org/2.7/library/functions.html?highlight=input#input" rel="nofollow"><code>input</code></a> tries to <a href="https://docs.python.org/2.7/library/functions.html?highlight=input#eval" rel="nofollow"><code>eval</code></a> the string you type in, treating it as though it is Python code, then returns the result of the evaluation. For example, if <code>result = input()</code> and I type in <code>2 + abs(-3)</code> then <code>result</code> will be equal to <code>5</code>.</p>

<p>When you enter the string <code>y</code> this is treated as a variable name. Since you haven't defined any variable named <code>y</code> you will get a <code>NameError</code>. Instead of <code>input</code> you want to use <a href="https://docs.python.org/2.7/library/functions.html#raw_input" rel="nofollow"><code>raw_input</code></a>, which just returns the input string without trying to evaluate it.</p>

<hr>

<p>In order to get your figures to display within the while loop you need to insert a short pause to allow the contents of the figure to be drawn before you continue executing your <code>while</code> loop. You could use <a href="http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.pause" rel="nofollow"><code>plt.pause</code></a>, which also takes care of updating the active figure.</p>

<pre><code>s = True
while s:

    number = input("Number: ")
    coords = np.array(np.random.randint(0, number, (number, 3)))

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.scatter(coords[:,0], coords[:,1], coords[:,2])
    plt.pause(0.1)

    cont = raw_input("Continue? (y/n)")
    if cont == 'n':
        s = False
</code></pre>

python numpy matplotlib