我试图写绘出使用椭圆r处的方程的对象的椭圆形路径的代码=α(1-E ^ 2)/(1个+ E * COS(THETA))。 我也想这个数据被投入用于其它用途的阵列。
from numpy import *#Imports Python mathematical functions library
import matplotlib.pyplot as plt #Imports plot library
from pylab import *
a = 5
e = 0.3
theta = 0
while theta <= 2*pi:
r = (a*(1-e**2))/(1+e*cos(theta))
print("r = ",r,"theta = ",theta)
plt.polar(theta, r)
theta += pi/180
plt.show()
该代码吐出来的R和θ正确的价值观,但情节是空白。 出现极坐标图窗口,但并没有什么绘制。
请帮忙。 提前致谢。
不要叫plt.polar
为每点一次。 相反,把它一次,所有的数据作为输入:
import numpy as np #Imports Python mathematical functions library
import matplotlib.pyplot as plt #Imports plot library
cos = np.cos
pi = np.pi
a = 5
e = 0.3
theta = np.linspace(0,2*pi, 360)
r = (a*(1-e**2))/(1+e*cos(theta))
plt.polar(theta, r)
print(np.c_[r,theta])
plt.show()
顺便说一句,numpy的可以做计算为双内胆,而是采用了while循环:
theta = np.linspace(0,2*pi, 360) # 360 equally spaced values between 0 and 2*pi
r = (a*(1-e**2))/(1+e*cos(theta))
这定义theta
和r
为numpy的阵列(而不是单个值)。
我认为你需要做的points.append([theta,r])
然后在最后plt.polar(points)
......,使一个还挺利落的设计太
from numpy import *#Imports Python mathematical functions library
import matplotlib.pyplot as plt #Imports plot library
from pylab import *
a = 5
e = 0.3
theta = 0
points = []
while theta <= 2*pi:
r = (a*(1-e**2))/(1+e*cos(theta))
print("r = ",r,"theta = ",theta)
points.append((theta, r))
theta += pi/180
#plt.polar(points) #this is cool but probably not what you want
plt.polar(*zip(*points))
plt.show()