Python:我的python图反映在y = 0.5行

时间:2013-06-22 12:16:20

标签: python matplotlib wolfram-mathematica

为什么我的Python图表会反映在y = 0.5行? Mathematica中的相同情节没有。我检查了方程5-10次,我没有看到差异。如果我在python图的前面放置一个-1,它将翻转并下拉1个单位到y = -0.5

此外,alphagbetag的定义也是正确的。

import numpy as np
import pylab

r1 = 1  #  AU Earth                                                                 
r2 = 1.524  #  AU Mars                                                              
deltanu = 75 * np.pi / 180  #  angle in radians                                     
mu = 38.86984154054163                                        

c = np.sqrt(r1 ** 2 + r2 ** 2 - 2 * r1 * r2 * np.cos(deltanu))

s = (r1 + r2 + c) / 2

am = s / 2


def g(a):
    alphag = 2* np.pi - 2 * np.arcsin(np.sqrt(s / (2 * a)))
    betag = -2 * np.arcsin(np.sqrt((s - c) / (2 * a)))
    return (np.sqrt(a ** 3 / mu)
            * (alphag - betag - (np.sin(alphag) - np.sin(betag)))
            - dt)


a = np.linspace(am, 2, 500000)
dt = np.linspace(0, 2, 500000)

fig = pylab.figure()
ax = fig.add_subplot(111)
ax.plot(a, g(a), color = 'r')
pylab.xlim((0.9, 2))
pylab.ylim((0, 2))

pylab.show()

的Python:

enter image description here

编辑2:

实际上我正在绘制2个情节,并且由于这些评论,我注意到有些事情甚至出现了。

我正在策划的两个地块是:

dt = np.sqrt(a ** 3 / mu) * (alpha - beta - (sin(alpha) - sin(beta)))

其中alpha2 * np.arcsin(np.sqrt(s / (2 * a)))2 * np.pi - 2 * np.arcsin(np.sqrt(s / (2 * a)))beta2 * np.arcsin(np.sqrt((s - c) / (2 * a)))或第一位为负。

In[13]:= r1 = 1;
r2 = 1.524;
dnu = 75 Degree;
mu = 38.86984154054163;

In[17]:= c = Sqrt[r1^2 + r2^2 - 2*r1*r2*Cos[dnu]]

Out[17]= 1.59176

In[18]:= s = (r1 + r2 + c)/2

Out[18]= 2.05788

In[19]:= alp = 2 \[Pi] - 2*ArcSin[Sqrt[s/(2*a)]];
bet = -2*ArcSin[Sqrt[(s - c)/(2*a)]];

In[22]:= Plot[
 Sqrt[a^3/mu]*(alp - bet - (Sin[alp] - Sin[bet])), {a, 0, 2}, 
 PlotRange -> {{.8, 2}, {0, 2}}]

这会产生:

enter image description here

alp2 = 2*ArcSin[Sqrt[s/(2*a)]];
bet2 = 2*ArcSin[Sqrt[(s - c)/(2*a)]];

Plot[Sqrt[a^3/mu]*(alp2 - bet2 - (Sin[alp2] - Sin[bet2])), {a, 0, 2}, 
 PlotRange -> {{.8, 2}, {0, 2}}]

enter image description here

所以Python代码与第一个Mathematica代码匹配,但是第二张图片的绘图和第二个Mathematica代码的python代码产生了第一张Mathematica图片的翻转图像。

1 个答案:

答案 0 :(得分:1)

我认为您只需要从Python代码中删除-dt

import numpy as np
import matplotlib.pyplot as plt

r1 = 1  #  AU Earth                                                                 
r2 = 1.524  #  AU Mars                                                              
deltanu = 75 * np.pi / 180  #  angle in radians                                     
mu = 38.86984154054163                                        

c = np.sqrt(r1 ** 2 + r2 ** 2 - 2 * r1 * r2 * np.cos(deltanu))

s = (r1 + r2 + c) / 2

am = s / 2


def g(a):
    alphag = 2 * np.pi - 2 * np.arcsin(np.sqrt(s / (2 * a)))
    betag = -2 * np.arcsin(np.sqrt((s - c) / (2 * a)))
    return (np.sqrt(a ** 3 / mu)
            * (alphag - betag - (np.sin(alphag) - np.sin(betag))))

def g2(a):
    alphag = 2 * np.arcsin(np.sqrt(s / (2 * a)))
    betag = 2 * np.arcsin(np.sqrt((s - c) / (2 * a)))
    return (np.sqrt(a ** 3 / mu)
            * (alphag - betag - (np.sin(alphag) - np.sin(betag))))


a = np.linspace(am, 2, 500000)
dt = np.linspace(0, 2, 500000)

fig, ax = plt.subplots(ncols=2)
ax[0].plot(a, g(a), color = 'r')
ax[1].plot(a, g2(a), color = 'r')
ax[0].set_xlim((0.9, 2))
ax[0].set_ylim((0, 2))
ax[1].set_xlim((0.9, 2))
ax[1].set_ylim((0, 2))

plt.show()

产量

enter image description here