绘制多个图但具有偏移范围python

时间:2013-05-14 03:17:52

标签: python numpy matplotlib

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

ValueError: operands could not be broadcast together with shapes (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()

1 个答案:

答案 0 :(得分:2)

我认为这就是你要做的事情:

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广播是你的朋友;)

如果你想得到真正的幻想:

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()