VectorField#
Qualified name: manim.mobject.vector\_field.VectorField
- class VectorField(func, color=None, color_scheme=None, min_color_scheme_value=0, max_color_scheme_value=2, colors=['#236B8E', '#83C167', '#FFFF00', '#FC6255'], **kwargs)[source]#
Bases:
VGroupA vector field.
Vector fields are based on a function defining a vector at every position. This class does by default not include any visible elements but provides methods to move other
Mobjects along the vector field.- Parameters
func (Callable[[np.ndarray], np.ndarray]) – The function defining the rate of change at every position of the VectorField.
color (Color | None) – The color of the vector field. If set, position-specific coloring is disabled.
color_scheme (Callable[[np.ndarray], float] | None) – A function mapping a vector to a single value. This value gives the position in the color gradient defined using min_color_scheme_value, max_color_scheme_value and colors.
min_color_scheme_value (float) – The value of the color_scheme function to be mapped to the first color in colors. Lower values also result in the first color of the gradient.
max_color_scheme_value (float) – The value of the color_scheme function to be mapped to the last color in colors. Higher values also result in the last color of the gradient.
colors (Sequence[Color]) – The colors defining the color gradient of the vector field.
kwargs – Additional arguments to be passed to the
VGroupconstructor
Methods
Scale the vector field to fit a coordinate system.
Generate an image that displays the vector field.
Get an update function to move a
Mobjectalong the vector field.Generates a gradient of rgbas as a numpy array
Nudge a
Mobjectalong the vector field.Apply a nudge along the vector field to all submobjects.
Scale a vector field function.
Shift a vector field function.
Start continuously moving all submobjects along the vector field.
Stops the continuous movement started using
start_submobject_movement().Attributes
animateUsed to animate the application of any method of
self.animation_overridescolordepthThe depth of the mobject.
fill_colorIf there are multiple colors (for gradient) this returns the first one
heightThe height of the mobject.
n_points_per_curvesheen_factorstroke_colorwidthThe width of the mobject.
- fit_to_coordinate_system(coordinate_system)[source]#
Scale the vector field to fit a coordinate system.
This method is useful when the vector field is defined in a coordinate system different from the one used to display the vector field.
This method can only be used once because it transforms the origin of each vector.
- Parameters
coordinate_system (CoordinateSystem) – The coordinate system to fit the vector field to.
- get_colored_background_image(sampling_rate=5)[source]#
Generate an image that displays the vector field.
The color at each position is calculated by passing the positing through a series of steps: Calculate the vector field function at that position, map that vector to a single value using self.color_scheme and finally generate a color from that value using the color gradient.
- Parameters
sampling_rate (int) – The stepsize at which pixels get included in the image. Lower values give more accurate results, but may take a long time to compute.
- Returns
The vector field image.
- Return type
Image.Imgae
- get_nudge_updater(speed=1, pointwise=False)[source]#
Get an update function to move a
Mobjectalong the vector field.When used with
add_updater(), the mobject will move along the vector field, where its speed is determined by the magnitude of the vector field.- Parameters
speed (float) – At speed=1 the distance a mobject moves per second is equal to the magnitude of the vector field along its path. The speed value scales the speed of such a mobject.
pointwise (bool) – Whether to move the mobject along the vector field. See
nudge()for details.
- Returns
The update function.
- Return type
- get_vectorized_rgba_gradient_function(start, end, colors)[source]#
Generates a gradient of rgbas as a numpy array
- Parameters
start (float) – start value used for inverse interpolation at
inverse_interpolate()end (float) – end value used for inverse interpolation at
inverse_interpolate()colors (Iterable) – list of colors to generate the gradient
- Return type
function to generate the gradients as numpy arrays representing rgba values
- nudge(mob, dt=1, substeps=1, pointwise=False)[source]#
Nudge a
Mobjectalong the vector field.- Parameters
mob (Mobject) – The mobject to move along the vector field
dt (float) – A scalar to the amount the mobject is moved along the vector field. The actual distance is based on the magnitude of the vector field.
substeps (int) – The amount of steps the whole nudge is divided into. Higher values give more accurate approximations.
pointwise (bool) – Whether to move the mobject along the vector field. If False the vector field takes effect on the center of the given
Mobject. If True the vector field takes effect on the points of the individual points of theMobject, potentially distorting it.
- Returns
This vector field.
- Return type
Examples
Example: Nudging ¶
from manim import * class Nudging(Scene): def construct(self): func = lambda pos: np.sin(pos[1] / 2) * RIGHT + np.cos(pos[0] / 2) * UP vector_field = ArrowVectorField( func, x_range=[-7, 7, 1], y_range=[-4, 4, 1], length_func=lambda x: x / 2 ) self.add(vector_field) circle = Circle(radius=2).shift(LEFT) self.add(circle.copy().set_color(GRAY)) dot = Dot().move_to(circle) vector_field.nudge(circle, -2, 60, True) vector_field.nudge(dot, -2, 60) circle.add_updater(vector_field.get_nudge_updater(pointwise=True)) dot.add_updater(vector_field.get_nudge_updater()) self.add(circle, dot) self.wait(6)
- nudge_submobjects(dt=1, substeps=1, pointwise=False)[source]#
Apply a nudge along the vector field to all submobjects.
- Parameters
dt (float) – A scalar to the amount the mobject is moved along the vector field. The actual distance is based on the magnitude of the vector field.
substeps (int) – The amount of steps the whole nudge is divided into. Higher values give more accurate approximations.
pointwise (bool) – Whether to move the mobject along the vector field. See
nudge()for details.
- Returns
This vector field.
- Return type
- static scale_func(func, scalar)[source]#
Scale a vector field function.
- Parameters
func (Callable[[ndarray], ndarray]) – The function defining a vector field.
scalar (float) – The scalar to be applied to the vector field.
- Return type
Callable[[ndarray], ndarray]
Examples
Example: ScaleVectorFieldFunction ¶
from manim import * class ScaleVectorFieldFunction(Scene): def construct(self): func = lambda pos: np.sin(pos[1]) * RIGHT + np.cos(pos[0]) * UP vector_field = ArrowVectorField(func) self.add(vector_field) self.wait() func = VectorField.scale_func(func, 0.5) self.play(vector_field.animate.become(ArrowVectorField(func))) self.wait()
- Returns
The scaled vector field function.
- Return type
Callable[[np.ndarray], np.ndarray]
- Parameters
func (Callable[[ndarray], ndarray]) –
scalar (float) –
- static shift_func(func, shift_vector)[source]#
Shift a vector field function.
- Parameters
func (Callable[[ndarray], ndarray]) – The function defining a vector field.
shift_vector (ndarray) – The shift to be applied to the vector field.
- Returns
The shifted vector field function.
- Return type
Callable[[np.ndarray], np.ndarray]
- start_submobject_movement(speed=1, pointwise=False)[source]#
Start continuously moving all submobjects along the vector field.
Calling this method multiple times will result in removing the previous updater created by this method.
- Parameters
speed (float) – The speed at which to move the submobjects. See
get_nudge_updater()for details.pointwise (bool) – Whether to move the mobject along the vector field. See
nudge()for details.
- Returns
This vector field.
- Return type
- stop_submobject_movement()[source]#
Stops the continuous movement started using
start_submobject_movement().- Returns
This vector field.
- Return type