1

Python 2.7.11, Win 7, x64, Numpy 1.10.4, matplotlib 1.5.1

I ran the following script from an iPython console after entering %matplotlib qt at the command line

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()

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

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

...but the figures are just blank & unresponsive until I enter an input for cont then I get,

NameError: name 'y' is not defined

...and the whole thing crashes.

So what am I missing here?

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?

DrBwts
  • 3,470
  • 6
  • 38
  • 62
  • Running this in python 2.7 on Mac and I can't replicate your issue. One issue I did have was `number` needed to be converted to an int for the script to run. Other than that this code seemed to run fine for me. – Kieran Sep 22 '16 at 16:40

2 Answers2

1

I haven't replicated but when you input the 'y' or 'n'. try to put the single (or double quotes) are the y or n

to input strings without quotes. Use raw_input instead of input

as described here Python 2.7 getting user input and manipulating as string without quotations

Community
  • 1
  • 1
somesingsomsing
  • 3,182
  • 4
  • 29
  • 46
  • Yep that worked, any idea why the plot hangs whilst waiting for that input? – DrBwts Sep 22 '16 at 16:57
  • In fact if I keep entering 'y' all the figures initiate but none of them plot until the loop is exited by entering 'n' – DrBwts Sep 22 '16 at 17:09
1

input tries to eval the string you type in, treating it as though it is Python code, then returns the result of the evaluation. For example, if result = input() and I type in 2 + abs(-3) then result will be equal to 5.

When you enter the string y this is treated as a variable name. Since you haven't defined any variable named y you will get a NameError. Instead of input you want to use raw_input, which just returns the input string without trying to evaluate it.


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 while loop. You could use plt.pause, which also takes care of updating the active figure.

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
ali_m
  • 71,714
  • 23
  • 223
  • 298