4

I would like to plot a function of e and nu, where e is the eccentricity and nu the true anomaly. I am only looking at elliptical orbits so 0<e<1. However, when I try to plot them against each other, I have a shape error:

ValueError: operands could not be broadcast together with shapes (10) (5000)

I know this is because I only want 10 spaces for the eccentricity, but is there a way around this?

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
    Your for loops are really wonky, to the point where I am not strictly sure what you are trying to do here. Commented May 14, 2013 at 3:24
  • at any rate the error is coming from the line v = np.sqrt(e ** 2 + 2 * e * np.cos(nu) + 1) and has nothing to do with matplotlib and numpy in behaving correctly. Commented May 14, 2013 at 3:31
  • @tcaswell I want to make a plot for e = .1, .2, ... 1 and overlay them all on top of each other. Commented May 14, 2013 at 3:33
  • 1
    @dustin: e has shape (10) and nu has shape (5000)., so numpy can't figure out what you want to happen that gets fed to the sqrt. Neither can I. What calculation are you trying to do in this line? Commented May 14, 2013 at 3:43

1 Answer 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()
Sign up to request clarification or add additional context in comments.

6 Comments

the plot pops for a millisecond and then immediately closes. How can I stop this?
I am using emacs through the shebang line and I have in my bashrc an alias for ipython --pylab=qt
it sounds like your python session is exiting as soon as the script exits. (I use emacs + py-shell to keep an interactive shell around). You should ask another question about how to get your system set up correctly, as that is out of the scope of your original question.
whenever I ask setup questions here, I get downvoted and closed so that doesn't really work out.
I would suggest reading the documentation ones, reshape and playing around in an interactive shell.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.