【发布时间】:2017-01-31 08:15:21
【问题描述】:
Python 2.7.11、Win 7、x64、Numpy 1.10.4、matplotlib 1.5.1
在命令行输入%matplotlib qt 后,我从 iPython 控制台运行了以下脚本
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()
它在 3D 中绘制随机散点图。所以我认为将它放入一个while循环并在每次迭代中获得一个新数字是一件小事。
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
...但是这些数字只是空白且没有响应,直到我输入cont 然后我得到了,
NameError: name 'y' is not defined
...整个事情都崩溃了。
那么我在这里错过了什么?
编辑:考虑到下面水生挑战的答案。这些数字仍然挂起,直到退出循环,然后它们都被同时绘制。任何人都知道为什么情节没有在循环内完成?
【问题讨论】:
-
在 Mac 上的 python 2.7 中运行它,我无法复制您的问题。我确实遇到的一个问题是
number需要转换为 int 才能运行脚本。除此之外,这段代码对我来说似乎运行良好。
标签: python numpy matplotlib