Plotting Vectors (NumPy)
We use matplotlib
for plotting in python.
.arrow
The .arrow
in matplotlib
is used to plot an arrow which starts at an initial point, say \((a_1,a_2)\) and ends at a terminal point, say \((b_1, b_2)\). The syntax is .arrow(a_1,a_2,b_1,b_2)
. Note, this plots the vector \(\vec{v} = [b_1-a_1, b_2-a_2]\) starting at the point \([a_1,a_2]\).
Therefore, here we plot the vector \(\vec{v} = [1,2]\) starting from the origin \([0,0]\).
We can decorate it by adding a head.
To have some more control over the coordinate axis we'll use .subplots
. The first few lines of code just change the plot from a bounding box to a set of coordinate axis meeting at the origin. This is likely how you are used to seeing plots in your textbook. We then plot a collection of four vectors.
fig, ax = plt.subplots()
ax.axis([-5,5,-5,5])
# Move left y-axis and bottom x-axis to centre, passing through (0,0)
ax.spines['left'].set_position('zero')
ax.spines['bottom'].set_position('zero')
# Eliminate upper and right axes
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
vectors = np.array([[3,4],[1,1],[1,-2],[-2,2]]) # four vectors to plot
for i in range(len(vectors)):
ax.arrow(0, 0, vectors[i][0], vectors[i][1], head_width=0.2, head_length=0.2, fc='red', ec='black')
Next is an example of plotting a vector \(\vec{v}\) along with its linear transformation \(A\vec{v}\).
v = np.array([1,2])
A = np.array([[1,-3],[2,1]])
v2 = A@v
fig, ax = plt.subplots()
ax.axis([-7,5,-5,5])
# Move left y-axis and bottim x-axis to centre, passing through (0,0)
ax.spines['left'].set_position('zero')
ax.spines['bottom'].set_position('zero')
# Eliminate upper and right axes
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.arrow(0, 0, v[0], v[1], head_width=0.2, head_length=0.2, fc='black', ec='black')
ax.arrow(0, 0, v2[0], v2[1], head_width=0.2, head_length=0.2, fc='black', ec='black')
Here is an example of plotting a vector and its image under the rotation matrix.
theta = np.pi/3
Arot = np.array([[np.cos(theta), -np.sin(theta)],[np.sin(theta), np.cos(theta)]])
e1 = np.array([1,0])
v = Arot@e1
fig, ax = plt.subplots(figsize = (4,4))
ax.axis([-2,2,-2,2])
# Move left y-axis and bottim x-axis to centre, passing through (0,0)
ax.spines['left'].set_position('zero')
ax.spines['bottom'].set_position('zero')
# Eliminate upper and right axes
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.arrow(0, 0, e1[0], e1[1], head_width=0.1, head_length=0.1, fc='black', ec='black')
ax.arrow(0, 0, v[0], v[1], head_width=0.1, head_length=0.1, fc='black', ec='black')
.quiver
The .quiver
command in matplotlib.pyplot
is for plotting many vectors all on the same set of coordinate axes. Much like .arrow
it takes four inputs, the starting coordinates and the ending coordinates, only now these are lists (or arrays) for multiple vectors.
Here is an example where we plot three vectors
plt.quiver([0, 0, 0], [0, 0, 0], [1, -2, 4], [1, 2, -7], color=['r','b','g'], angles='xy', scale_units='xy', scale=1)
plt.xlim(-3, 5)
plt.ylim(-8, 3)
# define vectors to plot
V = np.array([[1,1], [-2,2], [4,-7]]) # array row vectors to plot
origin = np.array([[0, 0, 0],[0, 0, 0]]) # origin point
# set up axes for plot
fig, ax = plt.subplots(figsize = (4,4))
ax.axis([-3,5,-8,3])
# Move left y-axis and bottim x-axis to centre, passing through (0,0)
ax.spines['left'].set_position('zero')
ax.spines['bottom'].set_position('zero')
# Eliminate upper and right axes
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
# plot vectors
ax.quiver(*origin, V[:,0], V[:,1], color=['r','b','g'], angles='xy', scale_units='xy', scale=1)
Example
Use .quiver
to plot three vectors
and their images under the rotation matrix with \(\theta = 6\pi/7\).
theta = 6*np.pi/7
# rotation matrix
Arot = np.array([[np.cos(theta), -np.sin(theta)],[np.sin(theta), np.cos(theta)]])
V = np.array([[1,1], [1.75,0.5], [0.5,1]]) # array row vectors to plot
origin = np.array([[0, 0, 0],[0, 0, 0]]) # origin point
# construct array of rotated vectors
Vrot = (Arot@V.T).T
fig, ax = plt.subplots(figsize = (4,4))
ax.axis([-2,2,-2,2])
# Move left y-axis and bottim x-axis to centre, passing through (0,0)
ax.spines['left'].set_position('zero')
ax.spines['bottom'].set_position('zero')
# Eliminate upper and right axes
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.quiver(*origin, V[:,0], V[:,1], color='k', angles='xy', scale_units='xy', scale=1)
ax.quiver(*origin, Vrot[:,0], Vrot[:,1], color='gray', angles='xy', scale_units='xy', scale=1)