My first experiment with modern Python scientific plotting tools and astronomy, that inspired all of the rest of this activity, was showing the Atlanta Python Meetup how to illustrate what it means that Mars has a very elliptical orbit.
%pylab inline
import de405
import jplephem
from sgp4.ext import jday
j0 = jday(2013, 1, 1, 0, 0, 0)
print 'The Julian Day of Jan 1 2013:', j0
twoyears = np.arange(j0, j0 + 365.251 * 2.0, 0.25)
print 'Two years range from', twoyears[0], 'to', twoyears[-1]
eph = jplephem.Ephemeris(de405)
x, y, z = eph.position('mars', twoyears)
print 'The first x value is:', x[0], 'km'
A polar plot helps our eyes discern when an ellipse is close to circular, versus when its shape is strongly exaggerated.
# This function looks innocently scalar.
def length(*args):
return sqrt(sum(a*a for a in args))
a = 3
b = 4
print 'The hypotenuse of a 3-4 triangle is', length(a, b)
# But it also accepts arrays!
a = array([6, 11, 12, 13])
b = array([8, 60, 35, 84])
print 'Four more hypotenuses:', length(a, b)
print 'A 3D vector length:', length(3, 0, -4)
theta = np.arctan2(x, y)
r = length(x, y, z)
print theta.shape
print r.shape
# We are ready to see how the distance
# from Mars to the Sun varies over time!
polar(theta, r)
Python is a general-purpose programming language that lets us package up our logic inside of first-class functions.
def plot_planet(t):
x, y, z = eph.position(name, twoyears)
theta = np.arctan2(x, y)
r = length(x, y, z)
polar(theta, r)
for name in 'mercury', 'venus', 'earthmoon', 'mars':
plot_planet(name)