- numpy - matplotlib Physim

Plot a Vector

In this section you will practice and apply what you have learned using the vector simulations, plotting a simple vector and understanding its properties.

Below is a graph of an example vector that you will plot. Ask yourself these questions: What direction is it pointing? How can I calculate the magnitude of this vector? What are the vector's components? How can I plot the unit vector facing the same direction as this vector?


If you would like to see the full code for plotting the vector, click the Display Code button below:


# import required libraries import matplotlib.pyplot as plt import numpy as np def plotvector(start, end): # generated graph fig, ax = plt.subplots() prop = dict(arrowstyle="-|>,head_width=0.4,head_length=0.8", shrinkA=0,shrinkB=0,color='b',fill=True) plt.annotate("", xy=end, xytext=start, arrowprops=prop) # axis lengths ax.axis([-5, 5, -5, 5]) # axis labels and graph title ax.set(xlabel='x-axis', ylabel='y-axis', title='Single Vector') ax.grid() return plt start = [0,0] end = [1,2] plotvector(start, end)

Here is a code editor so that you can input your start and end points for the vector you will plot.


This is how the start and end inputs should look like:

start = [0, 0]

end = [1, 2]

plotvector is what creates a plot based on the start and end inputs.


start = [0,0] end = [3,4] plotvector(start, end)


Go Back to Vector Exercises Page