In this post, we visualize how a linear operation encoded by a 2D matrix transforms a vector space. As an example, consider the matrix
A=(21−11)
that transforms an arbitrary vector (xy) to a linear combination of the column vectors of A:
(uv)=(21−11)(xy)=x(21)+y(−11)
We can get a visual feel for this transformation by looking at a regular grid of points before and after the transformation:
But it’s fun to visualize this as an animation!
This post describes how to create such animations and uses them to visualize some common linear transforms. Click here to download the complete python script.
Strategy
Create a rectangular array of points in x-y space.
Map grid coordinates to colors that uniquely identify each point.
Generate a series of intermediate transforms that will “smoothly” transition from the original grid to the transformed grid.
Plot each of the intermediate transforms and save them as individual images.
Stitch images into a gif.
Setup
Start a python shell and import libraries:
Generate original and transformed grids
To construct a grid of points, we generate evenly spaced vectors along the x and y axes, and combine them together into a grid
Here, the x-axis values span from -4 to 4 and the y-axis value span from -3 to 3. By stacking the x-y pairs columnwise, we generate a 2-by-n rectangular grid of points:
xygrid=(−4−3−4−2……4344)
To generate the transformed grid, we perform a matrix multiplication:
uvgrid=A⋅xygrid
Plot grids
To plot the grid points, we will use the matplotlib function scatter that can apply a different color to each point. The following function transforms an (x,y) coordinate pair to an rgb color:
We map this function to the x-y coordinates to generate an array of rgb color, and then plot the x-y grid points:
which produces:
Similarly, we can plot the transformed grid:
Generate intermediate transforms
To create the animated version, we need a series of intermediate grids that will smoothly transition from the original grid to the transformed grid. One way to achieve this is by constructing a series of 2-by-2 matrices that interpolate between the identity matrix I=(1001) and the target matrix A=(21−11). Supose we want to do this in n steps. Then the jth matrix in this sequence is:
Aj=I+nj(A−I)=(1+j/nj/n−j/n1)
where j=0…n. The matrix product
Aj⋅xygrid
computes grid coordinates for the jth intermediate transform. The following code block generates all the intermediate grids for a given target matrix, and returns the results in a 3d array:
Plot intermediate transforms
Next we plot each of the intermediate grids on a common axis. To construct the animated version, we need to save each of these intermediate plots as an image file. The following code block defines a function that generates a series of image files with the filename frame-xx.png and saves them in a subdirectory. We apply this function to the array of intermediate grid coordinates that we generated above:
Create animation
To stitch the image sequence into an animation, we use the ImageMagick, a cross-platform image manipulation library. The following code block performs this operation by making a system call to the convert script that is part of ImageMagick. (Note: This only works on linux or OS X and requires ImageMagick to be available at the command line)
which produces the following gif:
More examples
We can repeat this process for any 2d linear transformation. Below are some commmon linear transformation visualized as animations:
Rotation
Clockwise rotation by an angle θ is encoded by the rotation matrix(cos(θ)sin(θ)−sin(θ)cos(θ))
Shear
A shear matrix of the form (10λ1) stretches the grid along the x axis by an amount proportional to the y coordinate of a point.