Data transformation with python

I have a log-scaled dataset, and I want to transform the data to measure the density of the dataset in each group. I made a scatterplot with dashed support lines. Its only purpose is to visualize the data.

enter image description here

I need a transformation something like this.

enter image description here

Do you have an idea, how do I rotate the data?

Here is a little sample about the data:

import matplotlib.pyplot as plt

x = [0.80212184, 2.47838352, 1.04379475, 1.42871703, 2.34573573,
       1.80048663, 1.51099012, 2.47128927, 1.89730346, 1.6375945 ,
       0.77876967, 0.76479765, 0.30721789, 2.90775006, 2.91452946]
y= [7.568806  , 6.63653933, 7.57696614, 8.471096  , 8.75421651,
       8.55456059, 8.1826057 , 8.62866297, 8.45774376, 8.26221853,
       7.84199122, 7.60613018, 8.31732613, 7.09044142, 7.08366203]
df = pd.DataFrame(np.array([x,y]).T, columns= ['x','y'])
supportlineY = [8.5,6.5]
supportline1X = [0,1.7]
supportline2X = [0.5,2.2]
supportline3X = [1,2.7]
supportline4X = [1.5,3.2]
supportline5X = [2,3.7]

plt.scatter(df['x'],df['y'])
plt.plot(supportline1X,supportlineY)
plt.plot(supportline2X,supportlineY)
plt.plot(supportline3X,supportlineY)
plt.plot(supportline4X,supportlineY)
plt.plot(supportline5X,supportlineY)

Upvotes: 1

Views: 262

Answers (1)

perl
perl

Reputation: 9941

You can multiply the coordinates by the rotation matrix:

# sample dataframe
df = pd.DataFrame({
    'x': np.arange(100),
    'y': np.arange(100) + np.random.randint(-10, 10, 100)})

# rotation angle, degrees
theta_deg = -45
theta = theta_deg / 180 * np.pi

# rotation matrix
rot = np.array([
    [np.cos(theta), -np.sin(theta)],
    [np.sin(theta), np.cos(theta)]
])

# rotated data
df_rot = pd.DataFrame(
    df[['x', 'y']].values.dot(rot),  # A * rot
    columns=['x', 'y'])

# plot
fig, ax = plt.subplots()
df.plot.scatter('x', 'y', color='green', ax=ax)
df_rot.plot.scatter('x', 'y', color='red', ax=ax)

Output:

chart

You'll need to find the right theta_deg angle for your data, of course, but since you already have support lines, I would assume that is not a problem.

Upvotes: 1

Related Questions