Rotate timeseries values around an angle with Python

Question:

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.

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?

Asked By: jamesB

||

Answers:

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

Answered By: freemangifts
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.