我真的很喜欢编程...
但这是我的问题:
我无法发布图像,但我想要的图是一个“冠”(两个半径为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个圆。
抱歉,我知道这真是一个微不足道的问题,
由于
答案 0 :(得分:4)
在我的回答中,我将使用两个库:
import numpy as np
import pylab
我相信这些是您设置中的常量:
r_a = 0.50
r_b = 0.75
circles = 6
lines = 50
origin = (0, 0)
首先,绘制圈子:
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()
结果:
(导入库并设置常量后,如上所述。)首先,计算点位置:
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中关于绘制线条的最后一点。我会在这里为所有未来的读者保留所有这些其他答案。