M_Z
M_Z

Reputation: 201

Plotting 3D vectors using matplotlib

I followed this thread in order to implement plotting 3D vectors.

When running the code with the given examples of vectors it seems fine, but when I try to use my own vectors I'm getting vectors without direction:

enter image description here

I'm trying to understand what is wrong with my data.

def plot_vectors(vectors):
fig = plt.figure()
# ax = fig.gca(projection='3d')
# ax = Axes3D(fig)
ax = fig.add_subplot(111, projection='3d')

for vector in vectors:
    # v1 = np.array([vector[0], vector[1], vector[2]])
    v_length = np.linalg.norm(vector)

    x = vector[0]
    y = vector[1]
    z = vector[2]

    # Make the direction data for the arrows
    u = np.sin(np.pi * x) * np.cos(np.pi * y) * np.cos(np.pi * z)
    v = -np.cos(np.pi * x) * np.sin(np.pi * y) * np.cos(np.pi * z)
    w = (np.sqrt(2.0 / 3.0) * np.cos(np.pi * x) * np.cos(np.pi * y) * np.sin(np.pi * z))

    ax.quiver(x, y, z, u, v, w, pivot='tail', length=v_length, arrow_length_ratio=0.3 / v_length)
    # ax.quiver(vector[0], vector[1], vector[2], vector[3], vector[4], vector[5],
    #           pivot='tail', length=vlength, arrow_length_ratio=0.3 / v_length)

# ax.set_xlim([-4, 4])
# ax.set_ylim([-4, 4])
# ax.set_zlim([0, 4])
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.show()

run it with these vectors for example:

plot_vectors([[95.38065011522669, -15.867331426507993, 267.4801091009956],
          [-52.379559088282384, -1.0021180591632532, 163.80875985057938]])

Upvotes: 0

Views: 1427

Answers (1)

Andrew Pye
Andrew Pye

Reputation: 622

I ran your code and looks like using .3 / v_length for the arrow_length_ratio yields a super tiny arrow head for your values of x, y, and z. I would use a different calculation here... perhaps something like .001 * v_length will work in this case. I would play around with it until you find something that you like and that works for all your data!

Upvotes: 1

Related Questions