vector_field#

k3d.factory.vector_field(vectors: List | ndarray | Tuple, colors: List[int] = None, origin_color: int | None = None, head_color: int | None = None, color: int = 255, use_head: bool = True, head_size: float = 1.0, scale: float = 1.0, line_width: float = 0.01, name: str | None = None, group: str | None = None, custom_data: Dict[str, Any] | None = None, compression_level: int = 0, visible: bool = True, **kwargs: Any) → VectorField[source]#

Create a VectorField drawable for plotting a regular grid of arrows.

Parameters:
  • vectors (array_like) – Array of (dx, dy, dz) components on a 2D or 3D grid.

  • colors (array_like, optional) – Twice the length of vectors array of int: packed RGB colors (0xff0000 is red, 0xff is blue). The array has consecutive pairs (origin_color, head_color) for vectors in row-major order. Default is None.

  • origin_color (int, optional) – Packed RGB color of the origins (0xff0000 is red, 0xff is blue) when colors is empty. Default is None.

  • head_color (int, optional) – Packed RGB color of the vector heads (0xff0000 is red, 0xff is blue) when colors is empty. Default is None.

  • color (int, optional) – Packed RGB color of the vectors (0xff0000 is red, 0xff is blue), used for whichever of origin_color and head_color is not given. Both multiply colors. Default is 255, or white when colors is given.

  • use_head (bool, optional) – Whether vectors should display an arrow head. Default is True.

  • head_size (float, optional) – The size of the arrow heads. Default is 1.0.

  • scale (float, optional) – Scale factor applied to every vector. Default is 1.0.

  • line_width (float, optional) – Width of the vector segments. Default is 0.01.

  • name (str, optional) – A name of the object. Default is None.

  • group (str, optional) – A name of a group. Default is None.

  • custom_data (dict, optional) – An object with custom data attached to object. Default is None.

  • compression_level (int, optional) – Level of compression [-1, 9]. Default is 0.

  • visible (bool, optional) – Whether the object is drawn. Default is True.

  • **kwargs – Additional keyword arguments passed to process_transform_arguments.

Returns:

The created VectorField object.

Return type:

VectorField

See also

Examples#

Basic#

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,
                                    color=0xed6a5a,
                                    head_size=1.5,
                                    scale=2,
                                    bounds=[-1, 1, -1, 1, -1, 1])

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

Colormap#

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,
                                    head_size=1.5,
                                    scale=2,
                                    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
plot.display()