Reputation: 67
I have been trying to figure an easy way to define u,v,w
of mpl_toolkits.mplot3d.Axes3D.quiver
such that all the quivers (from all over the 3D scatter plot) are all pointing towards the origin. Many thanks for all help rendered!!
Upvotes: 0
Views: 1012
Reputation: 10328
You can do this fairly easily by starting with unit-length quivers:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
n = 25
# Generate some random data
x = (np.random.random(n) - 0.5) * 10
y = (np.random.random(n) - 0.5) * 10
z = (np.random.random(n) - 0.5) * 10
r = np.sqrt(np.power(x,2) + np.power(y,2) + np.power(z,2))
# Create unit-length quivers by dividing out the magnitude
u = - x / r
v = - y / r
w = - z / r
ax.quiver(x, y, z, u, v, w)
plt.show()
And then if you want quivers of a certain size or if you have an array of sizes you can simply multiply this in when creating u
, v
, and w
:
sizes = np.random.random(n) * 4
u = -x / r * sizes
v = -y / r * sizes
w = -z / r * sizes
ax.quiver(x, y, z, u, v, w)
plt.show()
This should also support other methods of analytical sizing - provided you calculate the r
vectors correctly.
Upvotes: 1