在图表上叠加n(用户生成的)点

时间:2015-05-21 15:32:21

标签: python matplotlib graph points

print "Enter the amount of points you would like to generate to estimate Pi"
n = raw_input()

plt.xlim((0,1))
plt.ylim((0,1))

x = numpy.linspace(0,1,500)
y = numpy.sqrt(1-x**2)

for i in range(int(n)):
    xcoordinate = random.random()
    ycoordinate = random.random()

plt.scatter(xcoordinate, ycoordinate)
plt.plot(x,y,'g')
plt.show()

我试图在此图表中分散n个随机点。相反,我只得到一个随机点。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您可以将xcoordinatexcoordinate转换为列表,然后在其中添加随机点。

xcoordinate=[]
ycoordinate=[]
for i in range(int(n)):
    xcoordinate.append(random.random())
    ycoordinate.append(random.random())

其余代码保持不变。

enter image description here