【问题标题】:Plotting elliptical orbits绘制椭圆轨道
【发布时间】:2012-09-04 17:33:34
【问题描述】:

我正在尝试编写一个代码,使用椭圆 r=a(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 和 theta 输出正确的值,但绘图是空白的。极坐标图窗口出现,但没有绘制任何内容。

请帮忙。提前致谢。

【问题讨论】:

  • 一个很快就很明显的缺陷是 theta 增加了 180 度(以弧度为单位) - 你不想要更小的步幅,比如 1 度吗?

标签: python matplotlib astronomy orbital-mechanics


【解决方案1】:

不要为每个点调用一次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))  

这将 thetar 定义为 numpy 数组(而不是单个值)。

【讨论】:

  • +1 与我给出的答案相同:) 基本上......加上有关 linspace 的更好信息
【解决方案2】:

我认为你需要先做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()

【讨论】:

  • 我希望这是木星,但偏心度方式太大了...
  • 我们的答案基本相同,但我们的情节完全不同! :) 我对如何修复(你的还是我的?)有点困惑,但是由于 theta 从 0 到 2pi,所以情节不应该是一个单一的轨道吗?
  • 啊,如果您使用plt.polar(*zip(*points)),我们的图表将是相同的,所以plt.polar 接收两个参数而不是单个列表。当给定元组列表时,我不确定plt.polar 在做什么......
  • 大声笑哦,我以为这只是情节......我认为它看起来很整洁
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-05
相关资源
最近更新 更多