- numpy - matplotlib Physim

Plot Vector Cross Product

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

Below is a graph of two vectors that have undergone the cross product and third vector is plotted along with those two vectors, which is perpendicular to both of the smaller vectors. Ask yourself these questions: What direction are all of these 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 cross product vector, click the Display Code button below:


# import required libraries import matplotlib.pyplot as plt import numpy as np from mpl_toolkits.mplot3d import Axes3D def plot_vector_cross_product(vector_1, vector_2): fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.set_xlim(-20, 20) ax.set_ylim(-20, 20) ax.set_zlim(-20, 20) origin = np.array([0, 0, 0]) ax.quiver(*origin, *vector_1, color='red') ax.quiver(*origin, *vector_2, color='green') cross_product = np.cross(vector_1, vector_2) ax.text(vector_1[0], vector_1[1], vector_1[2],'Vector 1: (' + str(vector_1[0]) + ', ' + str(vector_1[1]) + ', ' + str(vector_1[2]) + ')', color='red') ax.text(vector_2[0], vector_2[1], vector_2[2], 'Vector 2: (' + str(vector_2[0]) + ', ' + str(vector_2[1]) + ', ' + str(vector_2[2]) + ')', color='green') ax.text(cross_product[0], cross_product[1], cross_product[2],'Cross Vector: (' + str(cross_product[0]) + ', ' + str(cross_product[1]) + ', ' + str(cross_product[2]) + ')', color='blue') ax.quiver(*origin, *cross_product, color='blue') ax.set_xlabel('x-axis') ax.set_ylabel('y-axis') ax.set_zlabel('z-axis') ax.set_title('Vector Cross Product Plot') ax.grid(True) return plt vector_1 = [2, 3, 0] vector_2 = [4, -1, 0] plot_vector_cross_product(vector_1, vector_2)

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


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

first vector = [2, 3, 0]

second vector = [4, -1, 0]

plot_vector_cross_product is what creates a plot of the two vectors inputted and the resulting third, cross product vector.


vector_1 = [2, 3, 0] vector_2 = [4, -1, 0] plot_vector_cross_product(vector_1, vector_2)


Go Back to Vector Exercises Page