Home > OS >  Rotate timeseries values around an angle with Python
Rotate timeseries values around an angle with Python

Time:10-06

I have a timeseries which for the sake of simplicity looks like this:

import pandas as pd

points = [0, 1, 0, 0, -1, 0]
df = pd.DataFrame(points)
df.plot(legend=False)

enter image description here

Now I want to rotate the values based on a specific angle, which will be between 0 and 90 degrees.

Note: I don't really need to be able to plot the values but I'm interested in the resulting values. (It's no longer a timeseries as it has multiple y-values for one x-value which makes plotting more elaborate, it still would be very helpful for personal debugging)

The resulting plot for a rotation of 90 degrees would look, according to matplotlib, like this:

from matplotlib import pyplot, transforms

base = pyplot.gca().transData
rot  = transforms.Affine2D().rotate_deg(90)
pyplot.plot(df, transform=rot base)
pyplot.show()

enter image description here

So I'm asking, what is the mathematical/programmatic way to get values rotated by an angle?

CodePudding user response:

Try below:

Rotate a point in XYZ space around the Z axis by γ angle:

import numpy as np
import math

def rotate_z(x, y, z, gamma):
    gamma = gamma * (np.pi / 180)
    x_r = np.cos(gamma)*x - np.sin(gamma)*y
    y_r = np.sin(gamma)*x   np.cos(gamma)*y
    z_r = z
    print(f"{(x, y, z)} rotate {gamma*(180/np.pi)} degrees around the Z-axis,result {(x_r, y_r, z_r)}")
    return x_r, y_r, z_r

The formula is below: rotate some degrees around the z-axis to get result

  • Related