在圆上生成点 [英] Generating points on a circle
本文介绍了在圆上生成点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
import random
import math
import matplotlib.pyplot as plt
def circle():
x = []
y = []
for i in range(0,1000):
angle = random.uniform(0,1)*(math.pi*2)
x.append(math.cos(angle));
y.append(math.sin(angle));
plt.scatter(x,y)
plt.show()
circle()
我写了上面的代码在单位圆上随机绘制1000个点.但是,当我运行这段代码时,由于某种原因,它绘制了一个椭圆形.这是为什么?
I've written the above code to draw 1000 points randomly on a unit circle. However, when I run this code, it draws an oval for some reason. Why is this?
推荐答案
It is a circle -- 问题是轴的纵横比不是 1,所以绘制时看起来像椭圆.要获得 1 的纵横比,您可以使用:
It is a circle -- The problem is that the aspect ratio of your axes is not 1 so it looks like an oval when you plot it. To get an aspect ratio of 1, you can use:
plt.axes().set_aspect('equal', 'datalim') # before `plt.show()`
这在演示中突出显示.
这篇关于在圆上生成点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文