title: Using arctan / arctan2 to plot a from 0 to 2π
body: <p>I am trying to replicate a plot in <em>Orbital Mechanics</em> by Curtis, but I just can't quite get it. However, I have made head way by switching to <code>np.arctan2</code> from <code>np.arctan</code>.</p>
  <p>Maybe I am implementing <code>arctan2</code> incorrectly?</p>
  <pre><code>import pylab
  import numpy as np
  
  e = np.arange(0.0, 1.0, 0.15).reshape(-1, 1)
  
  nu = np.linspace(0.001, 2 * np.pi - 0.001, 50000)
  M2evals = (2 * np.arctan2(1, 1 / (((1 - e) / (1 + e)) ** 0.5 * np.tan(nu / 2) -
             e * (1 - e ** 2) ** 0.5 * np.sin(nu) / (1 + e * np.cos(nu)))))
  
  fig2 = pylab.figure()
  ax2 = fig2.add_subplot(111)
  
  for Me2, _e in zip(M2evals, e.ravel()):
      ax2.plot(nu.ravel(), Me2, label = str(_e))
  
  pylab.legend()
  pylab.xlim((0, 7.75))
  pylab.ylim((0, 2 * np.pi))
  pylab.show()
  </code></pre>
  <p>In the image below, there are discontinuities popping up. The function is supposed to be smooth and connect at 0 and 2 pi in the y range of (0, 2pi) not touching 0 and 2pi.</p>
  <p><img src="https://i.stack.imgur.com/JY9BX.png" alt="Enter image description here" /></p>
  <p>Textbook plot and equation:</p>
  <p><img src="https://i.stack.imgur.com/2jfj5.jpg" alt="Enter image description here" /></p>
  <p><img src="https://i.stack.imgur.com/coYmE.jpg" alt="Enter image description here" /></p>
  <p>At the request of Saullo Castro, I was told that:</p>
  <blockquote>
  <p>The problem may lie in the arctan function which gives &quot;principle values&quot; as output.</p>
  </blockquote>
  <p>Thus, arctan(tan(x)) does not yield x if x is an angle in the second or third quadrant. If you plot arctan(tan(x)) from x = 0 to x = Pi, you will find that it has a discontinuous jump at x = Pi/2.</p>
  <p>For your case, instead of writing arctan(arg), I believe you would write arctan2(1, 1/arg) where arg is the argument of your arctan function. That way, when arg becomes negative, arctan2 will yield an angle in the second quadrant rather than the fourth.&quot;</p>