简体   繁体   English

在 python 中沿圆周方向绘制 n 个相等点

[英]plotting n number of equal points in circular direction in python

I am working on the task in which I have to make a circle which is having n number of equal parts.我正在做一个任务,我必须制作一个有 n 个相等部分的圆圈。 I am provided with centre and radius of the circle which is (0,0) and 4 respectively.我提供了圆的中心和半径,分别为 (0,0) 和 4。 To achieve this task, I have written below code,为了完成这个任务,我写了下面的代码,

parts = 36                     # desire number of parts of the circle          
theta_zero = 360/parts
R = 4                          # Radius

x_p = []
y_p = []

n = 0
for n in range(0,36):
    x_p.append(R * math.cos(n*theta_zero))
    y_p.append(R * math.sin(n*theta_zero))

However, after running this code, I got output like below which does not seem a coorect which I am suppose to have.但是,在运行此代码后,我得到了 output ,如下所示,这似乎不是我认为的正确。

在此处输入图像描述

Kindly let me know what I am doing wrong and give some suggestion of correct code.请让我知道我做错了什么,并给出一些正确代码的建议。 Thank you谢谢

Aside from the fact that you are generating numbers in degrees and passing them to a function that expects radians, there's a much simpler and less error-prone method for generating evenly-spaced coordinates:除了您以度数生成数字并将它们传递给需要弧度的 function 之外,还有一种更简单且不易出错的方法来生成均匀间隔的坐标:

t0 = np.linspace(0, 2 * np.pi, parts, endpoint=False)
x0 = R * np.cos(t0)
y0 = R * np.sin(t0)

endpoint=False ensures that you end up with 36 partitions rather than 35, since otherwise 0 and 2 * np.pi would overlap. endpoint=False确保您最终得到 36 个分区而不是 35 个,否则02 * np.pi会重叠。

If you wanted to connect the dots for your circle, you would want the overlap.如果你想为你的圆圈连接点,你会想要重叠。 In that case, you would do在这种情况下,你会做

t1 = np.linspace(0, 2 * np.pi, parts + 1)
x1 = R * np.cos(t1)
y1 = R * np.sin(t1)

Here is how you would plot a circle with the 36 sectors delineated:以下是您如何将 plot 画一个圆圈,圈出 36 个扇区:

plt.plot(x1, y1)
plt.plot(np.stack((np.zeros(parts), x0), 0),
         np.stack((np.zeros(parts), y0), 0))

Finally, if you want your circle to look like a circle, you may want to run plt.axis('equal') after plotting.最后,如果你想让你的圆圈看起来像一个圆圈,你可能想在绘图后运行plt.axis('equal')

Your circle is weird because math.cos and math.sin accept radians while you are passing degrees.你的圈子很奇怪,因为math.cosmath.sin在你通过学位时接受弧度。 You just need to convert the degrees to radians when calling the math functions.您只需在调用数学函数时将度数转换为弧度即可。

Like this:像这样:

parts = 36         
theta_zero = 360/parts
R = 4 

x_p = []
y_p = []

for n in range(0,36):
    x_p.append(R * math.cos(n*theta_zero /180*math.pi))
    y_p.append(R * math.sin(n*theta_zero /180*math.pi))

Result:结果:

圆圈

Alternatively changing theta_zero to 2*math.pi/parts would also work and would be slightly faster but it might be a little less intuitive to work with.或者将theta_zero更改为2*math.pi/parts也可以,并且会稍微快一些,但使用起来可能不太直观。

Also as @Mad Physicist mentioned you should probably add plt.axis('equal') to unstretch the image.同样正如@Mad Physicist 所提到的,您可能应该添加plt.axis('equal')来取消拉伸图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM