iliam14
iliam14

Reputation: 64

pyplot How to plot scatter and quiver in 3D on the same axes?

I want to print a scatter plot of points and to add their major directions.

So I have an array of vectors (in 3D) [x,y,z] to [ u,v,w] that I want to display and another array of points in 3D.

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt


fig = plt.figure()
ax = fig.gca(projection='3d')
ax.quiver(x, y, z, u, v, w, length=0.1, normalize=False)
# if i stop here I see the quiver
ax.scatter(X[:, 0], X[:, 1], X[:, 2])
plt.show() # here I see only the scatter

Upvotes: 0

Views: 1560

Answers (2)

Daniel
Daniel

Reputation: 263

Take a look here 3D plot, it is exactly what you want.

Upvotes: 0

anishtain4
anishtain4

Reputation: 2402

The mplot3d library is a simple plotting which mocks the shape of 3d plots rather than actually draw them. As a result you cannot have two clear plots in the same axes. You can see this clearly by plotting two surfaces, shifted slightly to one side. It plots one of them then plots the other one, so the depth will be all messed up. Saying that, this doesn't seem to be the problem with you. You haven't provided any information about your data, but for a dummy case I made, the plot seems reasonable: from mpl_toolkits.mplot3d import Axes3D

import matplotlib.pyplot as plt
fig = plt.figure();ax = fig.gca(projection='3d')
ax.quiver([0,0,0], [0,0,0], [0,0,0], [5,0,0], [0,5,0], [0,0,5], length=0.1, normalize=False)
ax.scatter([0,1,2,3], [1,2,1,2],[-1,0,1,2])

enter image description here Try this code and see if you get the same results or not? It might be that your quiver plot is much smaller than the range of the scatter plot, so you're missing it visually.

Upvotes: 2

Related Questions