5

我对编程真的很陌生...

但这是我的问题:

我无法发布图像,但我希望拥有的情节是“皇冠”(我的意思是两个半径为 a 的同心圆,从数学上讲真的很容易定义,但我怎么能用 python 程序做到这一点?

我想到了这样的事情:

def Fm1(X, Y):
    r =r = sqrt(1.*X**2+1.*Y**2)
    cos = 1.*X/r
    sin = 1.*Y/r
    teta = where( sin >= 0. , arccos(cos) , -arccos(cos) )
    teta = where(r == 0. , 0., teta)
    return r, teta


def F(r,teta):                                                                  
    X = r*cos(teta)                                                             
    Y = r*sin(teta)                                                             
    return X,Y

这些只是让您从笛卡尔坐标传递到极坐标的函数,然后:

r=sy.linspace(a,b,N+1) # radius division
t=sy.linspace(0,2.*pi,2**NN) #angle (theta) division
R,T=meshgrid(r,t) #creating a mesh

X,Y = F(R,T)#transform from polar to cartesian

#Plotting :
fig=plt.figure()
ax=fig.add_subplot(111)                                 
ax.plot(X, Y)
plt.show()

但结果是:同心多边形。我希望我在半径 a 到半径 b 和 2**NN 线(原点中心和给定角度)等距离处有 N+1 个圆。

对不起,我知道这真的是一个微不足道的问题,

谢谢

4

1 回答 1

4

在我的回答中,我将使用两个库

import numpy as np
import pylab

我相信这些是您设置中的常量:

r_a = 0.50
r_b = 0.75
circles = 6  
lines   = 50
origin = (0, 0)

选项 1:直接绘图

首先,画圆圈

for r in np.linspace(r_a, r_b, circles):
    pylab.gca().add_patch(pylab.Circle(origin, radius=r, 
                                       fill=False, color='black'))

然后画线

r_ab = np.array([r_a, r_b])
for theta in np.linspace(0, 2 * np.pi, lines):
    pylab.plot(np.cos(theta) * r_ab,
               np.sin(theta) * r_ab, color='red')

最后,显示

pylab.axis('scaled')
pylab.show()

结果:_

情节形象

选项 2:绘制段:

(在导入库并设置常量后,如上所述。)首先,计算点位置

r,t   = np.meshgrid(np.linspace(r_a, r_b, circles),
                    np.linspace(0, 2 * np.pi, lines))
x = r * np.cos(t)
y = r * np.sin(t)

然后绘制圆圈(就像你一样)并绘制线条

# Plot circles
pylab.plot(x, y)
# Plot lines (first and last x and y of each theta)
pylab.plot(np.vstack((x[:,0], x[:, -1])),
           np.vstack((y[:,0], y[:, -1])))

最后,显示

pylab.axis('scaled')
pylab.show()

结果:_

第二个选项结果

注意:毕竟,我认为您真正需要的只是选项 2 中关于绘制线条的最后一点。我会将所有其他答案的内容保留在这里以供任何未来的读者使用。

于 2012-12-05T17:56:40.613 回答