Plotting a Vector in 2D

linear algebra fundamentals

Posted by Joel on June 12, 2018

Plotting a vector in 2D cartesian coordinate

# Import NumPy and Matplotlib
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt

# Define vector v
v = np.array([1,1])

# Create axes of plot referenced as 'ax'
ax = plt.axes()

# Sets limit for plot x-axis
plt.xlim(-2, 2)

# Set limit plot for y-axis
plt.ylim(-1, 2)

# Set major ticks for x-axis
major_xticks = np.arange(-2, 3)
ax.set_xticks(major_xticks)

# Set major ticks for y-axis
major_yticks = np.arange(-1, 3)
ax.set_yticks(major_yticks)

# Create gridlines for only major tick marks
plt.grid(b=True, which='major')

# Plots ref dot at origin (0,0
ax.plot(0,0,'or')

# Plot the vector as blue arrow starting at origin 0,0
ax.arrow(0, 0, *v, color='b', linewidth=2.0, head_width=0.20, head_length=0.25)


# Displays final plot
plt.show()