title: Python Matplotlib: Turning orbit program into 3D
body: <p>I have finished my orbiting program in 2D, using real physics, and it is pretty cool. One thing I want to do now, is add a "z" force, acceleration, and velocity to the equation. I have done this, but now the program is just a vertical line orbiting a vertical line:</p>
  
  <p><img src="https://i.stack.imgur.com/0pi6t.png" alt="Planets are apparently vertical lines, unlike previously thought"></p>
  
  <p>Since theres no code for creating a sphere like there is for circle, I needed to use trig:</p>
  
  <p>Here's the code:</p>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  import math
  import matplotlib.animation as animation
  import pdb
  from mpl_toolkits.mplot3d import Axes3D
  
  er = 6378100*10#m
  mr = 1737400*10#m
  em = 5.97219*10**24#kg
  mm = 7.34767309*10**22#kg
  G = 6.67384*10**(-11)
  mv = -1023#m/s
  nts = 10000
  ts = 10000
  
  def simData():
      tmax = ts*nts
      jx = 0.0
      t = 0.0
      d = 384400000
      mx = d
      my = 0
      mz = 0
      vx = 0
      vy = -mv
      vz = 0
  
      while t &lt; tmax:
          Fg = G*(em*mm)/d**2
          Fgx = -Fg*mx/d
          Fgy = -Fg*my/d
          Fgz = -Fg*mz/d
          mAx = Fgx/mm
          mAy = Fgy/mm
          mAz = Fgz/mm
          vx = vx + mAx*ts
          vy = vy + mAy*ts
          vz = vz + mAz*ts
          mx = mx + vx*ts
          my = my + vy*ts
          mz = mz + vz*ts
  
          u, v = np.mgrid[0:2*np.pi:20j, 0:np.pi:10j]
          x=np.cos(u)*np.sin(v)+mx
          y=np.sin(u)*np.sin(v)+my
          z=np.cos(v)+mz
          ax.plot_wireframe(x, y, z, color="grey")
  
          d = math.sqrt(mx**2+my**2+mz**2)
          t = t + ts
          yield jx, t
  
  def simPoints(simData):
      jx, t = simData[0], simData[1]
      time_text.set_text(time_template%(t))
      line.set_data(t, jx)
      return line, time_text
  
  
  
  fig = plt.figure()
  ax = fig.add_subplot(111, projection='3d')
  ax.set_aspect("equal")
  line, = ax.plot([], [], 'bo', ms=10)
  
  eu, ev = np.mgrid[0:2*np.pi:20j, 0:np.pi:10j]
  eax=np.cos(eu)*np.sin(ev)
  eay=np.sin(eu)*np.sin(ev)
  eaz=np.cos(ev)
  ax.plot_wireframe(eax, eay, eaz, color="b")
  
  time_template = 'Time = %.1f s'    # prints running simulation time
  time_text = ax.text(0.05, 0.9, 0.9,'', transform=ax.transAxes)
  
  ani = animation.FuncAnimation(fig, simPoints, simData, blit=False,\
       interval=10, repeat=True)
  plt.show()
  </code></pre>
  
  <p>I have tested the trig for making circles without this program and it works. Also, any suggestions to making surfaced spheres instead of wireframe spheres?</p>