- numpy - matplotlib Physim

Plot Vector Subtraction

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

Below is a graph of two vectors subtracted from each other, and their resultant vector that you will plot. Ask yourself these questions: What direction are the vectors pointing? How can I calculate the magnitude of these vectors? What are each these vector's components?


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


# import required libraries import matplotlib.pyplot as plt def plot_vector_subtraction(vectors): fig, ax = plt.subplots() ax.set_xlim(-10, 10) ax.set_ylim(-10, 10) # Plot the vectors ax.annotate("", xy=[vectors[0][0], vectors[0][1]], xytext=[0, 0], arrowprops=dict(arrowstyle="-|>,head_width=0.4,head_length=0.8", shrinkA=0, shrinkB=0, color='black', fill=True)) ax.annotate("", xy=[vectors[0][0] - vectors[1][0], vectors[0][1] - vectors[1][1]], xytext=[vectors[0][0], vectors[0][1]], arrowprops=dict(arrowstyle="-|>,head_width=0.4,head_length=0.8", shrinkA=0, shrinkB=0, color='black', fill=True)) ax.annotate("", xy=[vectors[0][0] - vectors[1][0], vectors[0][1] - vectors[1][1]], xytext=[0, 0], arrowprops=dict(arrowstyle="-|>,head_width=0.4,head_length=0.8", shrinkA=0, shrinkB=0, color='red', fill=True)) ax.set_xlabel('x-axis') ax.set_ylabel('y-axis') ax.set_title('Vector Subtraction Plot') ax.grid(True) return plt vectors = [[5, 6], [1, 7]] plot_vector_subtraction(vectors)

Here is a code editor so that you can input two vectors each with their own x and y component coordinates.


This is how the two vector inputs inputs should look like:

first vector = [5, 6]

second vector = [1, 7]

plot_vector_subtraction is what creates a plot of the two vectors inputted subtracted from each other.


vectors = [[5, 6], [1, 7]] plot_vector_subtraction(vectors)


Go Back to Vector Exercises Page