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.

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. By mistake I copied different code. But Now I know the answer.
do you have any idea to set this view (without rotating by hand manually) by code only ?
Try to take a look to this answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.