2

I am trying to visualize two 3D column vectors using matplotlib. Here are the vector

v1 = (0, 2 , 1)
v2 = (2, 2, 0)

The visualization for the above vectors that I got from the book. enter image description here

[Source: Introduction to Linear Algebra, Fifth Edition (2016), by Gilbert Strang ]

I am trying to to visualize the same. Here is code and the plot.


import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as plticker
from mpl_toolkits.mplot3d import Axes3D

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

soa = np.array([[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0], [0, 0, 4, 0.5, 0.7, 0]])

X, Y, Z, U, V, W = zip(*soa)
ax.quiver(X, Y, Z, U, V, W)
ax.set_xlim([-1, 5])
ax.set_ylim([-1, 5])
ax.set_zlim([-1, 8])
plt.show()

View: enter image description here

I am not sure about the view. is it correct? Need help if I am wrong here.

Zephyr
  • 11,891
  • 53
  • 45
  • 80
Arijit Panda
  • 1,581
  • 2
  • 17
  • 36

1 Answers1

1

With the line:

soa = np.array([[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0], [0, 0, 4, 0.5, 0.7, 0]])

you are defining 4 3D vectors, 3 of which have 0 length, the last one starts from (0, 0, 4) and has components (0.5, 0.7, 0). So the code is compliant with the image you reported.
If you want to plot the vectors v1 and v2, both starting from origin, you should use:

soa = np.array([[0, 0, 0, 0, 2, 1], [0, 0, 0, 2, 2, 0]])

Complete Code

import numpy as np
import matplotlib.pyplot as plt


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

soa = np.array([[0, 0, 0, 0, 2, 1], [0, 0, 0, 2, 2, 0]])

X, Y, Z, U, V, W = zip(*soa)
ax.quiver(X, Y, Z, U, V, W)
ax.set_xlim([-1, 5])
ax.set_ylim([-1, 5])
ax.set_zlim([-1, 8])
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')

plt.show()

enter image description here

Zephyr
  • 11,891
  • 53
  • 45
  • 80