Search code examples
pythonnumpyrotational-matrices

How to Rotate Points from a Meshgrid and Preserve Orthogonality


When I use a rotation matrix to rotate points from a meshgrid, the points are no longer orthogonal. Using NumPy, how do I keep the gridlines perpendicular when rotating?

enter image description here

Here's the Python code that I'm using:

import numpy as np
import matplotlib.pyplot as plt

# Input
rot_degrees = 10
x = np.linspace(200, 600, 4)
y = np.linspace(200, 700, 5)
xv, yv = np.meshgrid(x, y)
pts = np.column_stack( (xv.flatten(), yv.flatten()) )

# Rotate points
radians = np.radians(rot_degrees)
rot_mat = np.array([[np.cos(radians),-np.sin(radians)],
                    [np.sin(radians), np.cos(radians)]])
einsum = np.einsum('ji, mni -> jmn', rot_mat, [pts])
pts_rotated = np.squeeze(einsum).T

# Output
plt.scatter(pts[:,0], pts[:,1], c='r')
plt.scatter(pts_rotated[:,0], pts_rotated[:,1], c='b')
plt.show()

Solution

  • How did you checked that the points are not orthogonal?

    If you just look at the plot, and it doesn't looks like, try setting the aspect ratio equal for both axis with

    plt.gca().set_aspect('equal')
    

    enter image description here