4

我想绘制一个 e 和 nu 的函数,其中 e 是偏心率,而 nu 是真正的异常。我只看椭圆轨道0<e<1。但是,当我尝试将它们相互绘制时,出现形状错误:

ValueError:操作数无法与形状一起广播 (10) (5000)

我知道这是因为我只想要 10 个空间用于怪癖,但有没有办法解决这个问题?

import numpy as np

e = np.arange(0, 1, 0.1)

vvals = [[] for i in range(len(e))]
nu = np.linspace(0, 2 * np.pi, 5000)


for i in e:
    for j in nu:
        i = float(i)
        j = float(j)
        v = np.sqrt(e ** 2 + 2 * e * np.cos(nu) + 1)
        i = int(i)
        vvals[i].append(v)


for i in e:
    pylab.plot(nu, vvals[i])


pylab.show()
4

1 回答 1

2

I think this is what you are trying to do:

import numpy as np

e = np.arange(0, 1, 0.1)
vvals = []
nu = np.linspace(0, 2 * np.pi, 5000)
for i in e:
    v = np.sqrt(i ** 2 + 2 * i * np.cos(nu) + 1)
    vvals.append(v)

for v in vvals:
    pylab.plot(nu, v)

pylab.show()

numpy broadcasting is your friend ;)

If you want to get really fancy:

import numpy as np

e = np.arange(0, 1, 0.1).reshape(-1, 1)
nu = np.linspace(0, 2 * np.pi, 5000).reshape(1, -1)
vvals = np.sqrt((e ** 2) * np.ones(nu.shape) + 2 * e * np.cos(nu) + 1)

for v, _e in zip(vvals, e.ravel()):
    pylab.plot(nu.ravel(), v, label=str(_e))

pylab.legend()    

pylab.show()
于 2013-05-14T03:44:45.940 回答