5

我正在尝试绘制以下内容!

from numpy import *
from pylab import *
import random

for x in range(1,500):
    y = random.randint(1,25000)
    print(x,y)   
    plot(x,y)

show()

但是,我不断得到一个空白图表(?)。只是为了确保程序逻辑正确,我添加了代码print(x,y),只是确认正在生成 (x,y) 对。

(x,y) 对正在生成,但没有绘图,我一直得到一个空白图。

有什么帮助吗?

4

1 回答 1

4

首先,我有时会通过这样做获得更好的成功

from matplotlib import pyplot

而不是使用pylab,尽管在这种情况下这不应该有所作为。

我认为您的实际问题可能是正在绘制点但不可见。使用列表一次绘制所有点可能会更好:

xPoints = []
yPoints = []
for x in range(1,500):
    y = random.randint(1,25000)
    xPoints.append(x)
    yPoints.append(y)
pyplot.plot(xPoints, yPoints)
pyplot.show()

为了使这更整洁,您可以使用生成器表达式:

xPoints = range(1,500)
yPoints = [random.randint(1,25000) for _ in range(1,500)]
pyplot.plot(xPoints, yPoints)
pyplot.show()
于 2010-04-07T07:55:07.943 回答