# How to Plot 3D Vectors in Python | Single & Multiple There are several ways to plot 3D vectors in Python, but one of the best ways is to use the matplotlib library, which provides a powerful and flexible interface for creating high-quality 3D plots.

Related | How to Plot 2D Vectors in Python

Here’s a step-by-step guide to creating a 3D vector plot using matplotlib:

## 1. Import the necessary libraries:

fig = plt.figure()ax = fig.add_subplot(111, projection='3d')

## 2. Create a 3D figure and set the axes:

fig = plt.figure()ax = fig.add_subplot(111, projection='3d')

## 3. Create the vectors you want to plot as numpy arrays. For example, to create two 3D vectors:

v1 = np.array([1, 2, 3])v2 = np.array([-2, 1, 4])

## 4. Add the vectors to the plot using the quiver() function. This function takes several arguments, including the x, y, and z coordinates of the vector’s starting point, the x, y, and z components of the vector itself, and the color and style of the arrowhead. For example, to add the two vectors to the plot:

#VECTOR 1ax.quiver(0, 0, 0, v1, v1, v1, color='r', arrow_length_ratio=0.1)#VECTOR 2ax.quiver(0, 0, 0, v2, v2, v2, color='b', arrow_length_ratio=0.1)

## 5. Set the limits of the x, y, and z axes so that the vectors are fully visible:

ax.set_xlim([-3, 3])ax.set_ylim([-3, 3])ax.set_zlim([-3, 3])

## 6. Add labels to the axes and the plot title:

ax.set_xlabel('X')ax.set_ylabel('Y')ax.set_zlabel('Z')plt.title('3D Vector Plot')

## 7. Finally, show the plot:

plt.show()

## Combined Python Code

				
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()

v1 = np.array([1, 2, 3])
v2 = np.array([-2, 1, 4])

#VECTOR 1
ax.quiver(0, 0, 0, v1, v1, v1, color='r', arrow_length_ratio=0.1)
#VECTOR 2
ax.quiver(0, 0, 0, v2, v2, v2, color='b', arrow_length_ratio=0.1)

ax.set_xlim([-3, 3])
ax.set_ylim([-3, 3])
ax.set_zlim([-3, 3])

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.title('3D Vector Plot')

plt.show()




This code will produce a 3D plot with two vectors, one in red and one in blue, originating from the origin.

You can customize the plot by changing the color and style of the arrowheads, or by adding more vectors to the plot.