pythonmatplotlib3d

How to plot vector addition in Matplotlib?


I am trying to plot vector addition and I am not getting the result as expected. This is what I have come so far: enter image description here

What I want is to connect the green line to the head of the two arrows. The code I am using:

import numpy as np
import matplotlib.pyplot as plt

u = np.array([1, 2, 3])   # vector u
v = np.array([5, 6, 2])   # vector v:
  


fig = plt.figure()
ax = plt.axes(projection = "3d")

start = [0,0,0]
ax.quiver(start[0],start[1],start[2],u[0],u[1],u[2],color='red')
ax.quiver(start[0],start[1],start[2],v[0],v[1],v[2])
ax.quiver(v[0],v[1],v[2],u[0],u[1],u[2],color="green")
ax.set_xlim([-1,10])
ax.set_ylim([-10,10])
ax.set_zlim([0,10])



plt.show()


Solution

  • it's vector addition, just add the vectors

    sum_vector = u+v
    ax.quiver(start[0], start[1], start[2], sum_vector[0], sum_vector[1], sum_vector[2],  color="green")