Here is Python code that uses NumPy and Seaborn to animate visualizations for the specified matrix transformations on a lattice ranging from -10 to 10 on both the x and y axes. The code interpolates from a square lattice to the transformed lattice and then back, looping continuously.
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
# Set Seaborn style
sns.set()
# Create a grid of points from -10 to 10
x_vals = np.arange(-10, 11)
y_vals = np.arange(-10, 11)
X, Y = np.meshgrid(x_vals, y_vals)
grid_points = np.vstack([X.ravel(), Y.ravel()]) # Shape is (2, N)
# Define transformation matrices
identity_matrix = np.array([[1, 0],
[0, 1]])
# Scale matrix (scales x by 2 and y by 0.5)
scale_matrix = np.array([[2, 0],
[0, 0.5]])
# Rotation matrix (rotates by 45 degrees)
theta = np.radians(45)
rotation_matrix = np.array([[np.cos(theta), -np.sin(theta)],
[np.sin(theta), np.cos(theta)]])
# Shear matrix (shears in the x-direction by 0.5)
shear_matrix = np.array([[1, 0.5],
[0, 1]])
# Scale and shear combination matrix
scale_shear_matrix = np.array([[1.5, 0.5],
[0.2, 1]])
# Random transformation matrix
np.random.seed(0) # For reproducibility
random_matrix = np.random.uniform(-1, 1, size=(2, 2))
# List of transformations
transformations = [
('Identity', identity_matrix),
('Scale', scale_matrix),
('Rotation', rotation_matrix),
('Shear', shear_matrix),
('Scale and Shear', scale_shear_matrix),
('Random', random_matrix)
]
# Function to animate transformations
def animate_transformation(start_matrix, end_matrix, grid_points, title, frames=40, interval=50):
# Generate interpolation values
t_values = np.concatenate((np.linspace(0, 1, frames // 2),
np.linspace(1, 0, frames // 2)))
transformed_data = []
for t in t_values:
# Interpolate between start and end matrices
interp_matrix = (1 - t) * start_matrix + t * end_matrix
# Apply transformation
transformed_points = np.dot(interp_matrix, grid_points)
transformed_data.append(transformed_points)
fig, ax = plt.subplots(figsize=(6, 6))
# Initialize scatter plot
sc = ax.scatter(transformed_data[0][0], transformed_data[0][1], s=10, color='blue')
# Set plot limits and properties
ax.set_xlim(-20, 20)
ax.set_ylim(-20, 20)
ax.set_aspect('equal')
ax.set_title(title)
# Update function for animation
def update(frame):
sc.set_offsets(transformed_data[frame].T)
return sc,
# Create animation
anim = FuncAnimation(fig, update, frames=len(transformed_data), interval=interval, blit=True)
plt.close(fig) # Prevents duplicate display of static plots
return anim
# Create and display animations for each transformation
animations = []
for name, matrix in transformations:
anim = animate_transformation(identity_matrix, matrix, grid_points, title=name)
animations.append((name, anim))
# Display animations (this code depends on the environment)
# If running in a Jupyter Notebook, you can display the animations inline
from IPython.display import HTML, display
for name, anim in animations:
display(HTML(f'<h3>{name} Transformation</h3>'))
display(HTML(anim.to_jshtml()))
Instructions:
-
Environment: This code is designed to run in a Jupyter Notebook or IPython environment where animations can be displayed inline.
-
Dependencies: Ensure you have the required libraries installed:
pip install numpy seaborn matplotlib
Explanation:
-
Grid Creation:
- A grid of points (
grid_points) is created using numpy.meshgrid, spanning from -10 to 10 on both axes.
-
Transformation Matrices:
- Six different 2x2 matrices are defined to represent the identity, scale, rotation, shear, scale & shear combination, and a random transformation.
-
Animation Function:
animate_transformation interpolates between the identity matrix and each transformation matrix using linear interpolation over time.
- The function creates an animation using
matplotlib.animation.FuncAnimation, which updates the positions of the grid points according to the interpolated transformation matrices.
- The scatter plot is initialized once, and the
update function updates the positions of the points for each frame to create a smooth animation.
-
Displaying Animations:
- In a Jupyter Notebook, the animations are displayed inline using
HTML and to_jshtml() methods.
- Each animation shows the grid transforming smoothly from the identity (original grid) to the transformed grid and back.
Sample Output:
When you run this code in a Jupyter Notebook, you will see animations like the following for each transformation:
-
Identity Transformation:
The grid remains unchanged throughout the animation.
-
Scale Transformation:
The grid stretches along the x-axis (by a factor of 2) and compresses along the y-axis (by a factor of 0.5), then returns to its original shape.
-
Rotation Transformation:
The grid rotates 45 degrees counter-clockwise and then rotates back.
-
Shear Transformation:
The grid shears along the x-axis by a factor of 0.5, then returns to its original shape.
-
Scale and Shear Transformation:
The grid undergoes a combined scale and shear transformation, showing both effects simultaneously.
-
Random Transformation:
The grid transforms according to a randomly generated matrix, creating a unique distortion.
Feel free to adjust the parameters, such as the number of frames or the interval between frames, to control the speed and smoothness of the animations. You can also modify the transformation matrices to explore other types of linear transformations.