- numpy - matplotlib Physim

Plot Vector Addition

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

Below is a graph of two vectors added together, 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 addition vector, click the Display Code button below:


# import required libraries import matplotlib.pyplot as plt def plot_vector_addition(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 Addition Plot') ax.grid(True) return plt vectors = [[2, 3], [4, -1]] plot_vector_addition(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 = [2, 3]

second vector = [4, -1]

plot_vector_addition is what creates a plot of the two vectors inputted added together.


vectors = [[2, 3], [4, -1]] plot_vector_addition(vectors)


Go Back to Vector Exercises Page