k3d.factory.vector_field(vectors, colors=[], origin_color=None, head_color=None, color=255, use_head=True, head_size=1.0, scale=1.0, line_width=0.01, name=None, group=None, custom_data=None, compression_level=0, **kwargs)[source]#

Create a VectorField drawable for displaying dense 2D or 3D grids of vectors of same dimensionality.

By default, the origins of the vectors are assumed to be a grid inscribed in the -0.5 < x, y, z < 0.5 cube or -0.5 < x, y < 0.5 square, regardless of the passed vector field shape, like aspect ratio.

Different grid size, shape and rotation can be obtained using kwargs

  • vector_field(..., bounds=[-pi, pi, -pi, pi, 0, 1])

  • vector_field(..., scaling=[scale_x, scale_y, scale_z])

For sparse (i.e. not forming a grid) 3D vectors, use vectors.

  • vectors (array_like) – Vector field of shape (L, H, W, 3) for 3D fields or (H, W, 2) for 2D fields.

  • colors (list, optional) –

    Array of Hex colors of vectors, by default [].

    The array has consecutive pairs (origin_color, head_color) for vectors in row-major order.

  • origin_color (int, optional) – Hex color of vector origins when colors is empty, by default None.

  • head_color (int, optional) – Hex color of vector heads when colors is empty, by default None.

  • color (int, optional) – Hex color of the vectors when colors is empty, by default _default_color.

  • use_head (bool, optional) – Display vector heads, by default True.

  • head_size (float, optional) – Vector heads size, by default 1.0.

  • scale (float, optional) – Scale factor for the vector lengths, by default 1.0.

  • line_width (float, optional) – Width of vector segments, by default 0.01.

  • name (str, optional) – Object name, by default None.

  • group (str, optional) – Name of a group, by default None.

  • custom_data (dict) – A object with custom data attached to object.

  • compression_level (int, optional) – Level of data compression [-1, 9], by default 0.

  • **kwargs – For other keyword-only arguments, see process_transform_arguments.


VectorField Drawable.

Return type


See also



import k3d
import numpy as np

def f(x, y):
    return np.sin(y), np.sin(x)

H = W = 10

vectors = np.array([[f(x, y) for x in range(W)] for y in range(H)]).astype(np.float32)
plt_vector_field = k3d.vector_field(vectors,
                                    bounds=[-1, 1, -1, 1, -1, 1])

plot = k3d.plot(grid_auto_fit=False)
plot += plt_vector_field


import k3d
import numpy as np
from k3d.colormaps import matplotlib_color_maps
from k3d.helpers import map_colors
from numpy.linalg import norm

p = np.linspace(-1, 1, 10)

def f(x, y, z):
    return y * z, x * z, x * y

vectors = np.array([[[f(x, y, z) for x in p] for y in p] for z in p]).astype(np.float32)
norms = np.apply_along_axis(norm, 1, vectors.reshape(-1, 3))

plt_vector_field = k3d.vector_field(vectors,
                                    bounds=[-1, 1, -1, 1, -1, 1])

colors = map_colors(norms, matplotlib_color_maps.Turbo, [0, 1]).astype(np.uint32)
plt_vector_field.colors = np.repeat(colors, 2)

plot = k3d.plot()
plot += plt_vector_field