I'm working with a data set of accelerations using python 2.7, in order to find the angle, I'm using arctan2(y,x). My problem is that, while my data can rotate beyond pi, the output of arctan2(y,x) is bounded between pi and -pi. This means any time I go above pi, I suddenly have a dramatic jump in my data and loop to negative pi.

I'm trying to figure out an elegant solution to fix this and make my graph continuous. I've implemented a simple check where if I even jump more than about 80% of the way, I assume I've gone out of bounds and start adding 2 pi to every subsequent data point. This works but feels very clunky. Is there a more elegant way to implement this? Or is this the best I can do?

Thank you ^\_^

```
for index in range(1,(len(x_data))):
    new_angle = math.atan2((y_data[index]), (x_data[index]))
    if (new_angle - angle[index-1]) > 5:
        new_angle = new_angle - 6.28
    if (new_angle - angle[index-1]) < -5:
        new_angle = new_angle + 6.28
    angle.append(new_angle)

```

Green shows what I want, black shows what I get
[![enter image description here](https://i.stack.imgur.com/YQvY0.png)](https://i.stack.imgur.com/YQvY0.png)