Skip to content

Plotting Vectors (SymPy)

import numpy as np
from sympy import *
import matplotlib.pyplot as plt

We use matplotlib for plotting in python. We also have to convert SymPy matrices to NumPy arrays prior to plotting. Therefore, we prefer to define vectors as NumPy arrays if we intend to just plot them. However, if we want to apply transformation first we define the vectors and matrices as SymPy objects, create the transformed objects, then convert to NumPy arrays before plotting.

.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]\).

plt.arrow(0,0,1,2)

Graph Graph

We can decorate it by adding a head.

plt.arrow(0, 0, 1, 2, head_width=0.05, head_length=0.05, fc='red', ec='black')

Graph Graph

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')

Graph Graph

Next is an example of plotting a vector \(\vec{v}\) along with its linear transformation \(A\vec{v}\).

v = Matrix([1,2])
A = Matrix([[1,-3],[2,1]])
v2 = A*v

# convert from SymPy matrix to NumPy array
v_np = np.array(v.tolist()).astype(np.float64)
v2_np = np.array(v2.tolist()).astype(np.float64)

# plot vectors
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_np[0,0], v_np[1,0], head_width=0.2, head_length=0.2, fc='black', ec='black')
ax.arrow(0, 0, v2_np[0,0], v2_np[1,0], head_width=0.2, head_length=0.2, fc='black', ec='black')

Graph Graph

Here is an example of plotting a vector and its image under the rotation matrix.

theta = np.pi/3
Arot = Matrix([[cos(theta), -sin(theta)],[sin(theta), cos(theta)]])
e1 = Matrix([1,0])
v = Arot*e1

# convert from SymPy matrix to NumPy array
e1_np = np.array(e1.tolist()).astype(np.float64)
v_np = np.array(v.tolist()).astype(np.float64)

# plot vectors
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_np[0,0], e1_np[1,0], head_width=0.1, head_length=0.1, fc='black', ec='black')
ax.arrow(0, 0, v_np[0,0], v_np[1,0], head_width=0.1, head_length=0.1, fc='black', ec='black')

Graph Graph

.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

\[ \vec{v_1} = \begin{bmatrix} 1 \\ 1 \end{bmatrix} , \quad \vec{v_2} = \begin{bmatrix} -2 \\ 2 \end{bmatrix} , \quad \vec{v_3} = \begin{bmatrix} 4 \\ -7 \end{bmatrix} \]
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)

Graph Graph

# 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)

Graph Graph

Example

Use .quiver to plot three vectors

\[ \vec{v_1} = \begin{bmatrix} 1 \\ 1 \end{bmatrix} , \quad \vec{v_2} = \begin{bmatrix} 1.75 \\ 0.5 \end{bmatrix} , \quad \vec{v_3} = \begin{bmatrix} 0.5 \\ 1 \end{bmatrix} \]

and their images under the rotation matrix with \(\theta = 6\pi/7\).

theta = 6*np.pi/7
# rotation matrix
Arot = Matrix([[cos(theta), -sin(theta)],[sin(theta), cos(theta)]])

V = Matrix([[1,1], [7/4,1/2], [1/2,1]]) # 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

# convert from SymPy matrix to NumPy array
V_np = np.array(V.tolist()).astype(np.float64)
Vrot_np = np.array(Vrot.tolist()).astype(np.float64)

# plot vectors
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_np[:,0], V_np[:,1], color='k', angles='xy', scale_units='xy', scale=1)
ax.quiver(*origin, Vrot_np[:,0], Vrot_np[:,1], color='gray', angles='xy', scale_units='xy', scale=1)

Graph Graph