-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add plot_field function that shows effect of a linear transformation on
a square field of points
- Loading branch information
Showing
3 changed files
with
56 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
import matplotlib as mpl | ||
|
||
def plot_field (matrix=np.eye(2), field_width=3, point_spacing=0.5, | ||
preserve_axes=True, colormap='plasma'): | ||
|
||
''' Plot the outputs of a linear transform on a square field of points | ||
Generates a uniformly spaced field of points in a 2-D square, | ||
applies the linear transform to the points, and plots the | ||
field of output points | ||
Parameters | ||
---------- | ||
matrix : ndarray | ||
2D matrix that specifies the linear transform | ||
field_width: number | ||
size of input point field in each dimension | ||
point_spacing: number | ||
spacing between field points in each dimension | ||
preserve_axes: boolean | ||
keep limits of output axes same as input space | ||
colormap: string | ||
Matplotlib colormap | ||
''' | ||
|
||
|
||
cmap = mpl.colormaps[colormap] | ||
|
||
xs = np.arange(-field_width, field_width + point_spacing, | ||
point_spacing) | ||
ys = xs | ||
|
||
for x in xs: | ||
for y in ys: | ||
angle = np.arctan2(y, x) | ||
if angle < 0: | ||
angle += 2*np.pi | ||
rgba = cmap(angle / (2 * np.pi) ) | ||
out = matrix @ np.array([x, y]) | ||
plt.scatter(out[0], out[1], 2, color=rgba) | ||
|
||
ax = plt.gca() | ||
ax.axis('equal') | ||
if preserve_axes: | ||
plt.xlim(-field_width, field_width) | ||
plt.ylim(-field_width, field_width) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters