#credits:https://github.com/Davide-sd
import numpy as np
fig = k3d.plot()
n = 20
x = np.linspace(-5, 5, n)
y = np.linspace(-5, 5, n)
z = np.linspace(-5, 5, n)
xx, yy, zz = np.meshgrid(x, y, z)
uu, vv, ww = zz, yy, xx
xx, yy, zz, uu, vv, ww = [t.flatten().astype(np.float32) for t in [xx, yy, zz, uu, vv, ww]]
scale = 0.5
magnitude = np.sqrt(uu**2 + vv**2 + ww**2)
vectors = np.array((uu, vv, ww)).T * scale
origins = np.array((xx, yy, zz)).T
colors = k3d.helpers.map_colors(magnitude, k3d.matplotlib_color_maps.Plasma, [])
vec_colors = np.zeros(2 * len(colors))
for i, c in enumerate(colors):
vec_colors[2 * i] = c
vec_colors[2 * i + 1] = c
vec_colors = vec_colors.astype(np.uint32)
vec = k3d.vectors(
origins = origins - vectors / 2,
vectors = vectors,
colors = vec_colors,
)
fig += vec
fig.display()