Animation engine for explanatory math videos with programmatic mathematical visualization capabilities
—
ManimGL provides comprehensive coordinate system support including 2D and 3D axes, number lines, complex planes, and function plotting capabilities. These tools enable precise mathematical visualization and graph-based animations.
Standard Cartesian coordinate systems with customizable ranges, styling, and labeling.
class Axes(VGroup):
def __init__(self, x_range=(-8, 8, 1), y_range=(-6, 6, 1), **kwargs):
"""
2D Cartesian coordinate axes.
Parameters:
- x_range: tuple, (x_min, x_max, x_step) for x-axis
- y_range: tuple, (y_min, y_max, y_step) for y-axis
- axis_config: dict, styling for both axes
- x_axis_config: dict, x-axis specific styling
- y_axis_config: dict, y-axis specific styling
- tips: bool, show axis arrows (default: True)
- tip_shape: ArrowTip class for axis ends
- include_ticks: bool, show tick marks
- tick_size: float, tick mark length
- include_numbers: bool, show axis labels
- numbers_with_elongated_ticks: list, which numbers get longer ticks
- numbers_to_exclude: list, numbers to skip labeling
- label_direction: np.array, direction for axis labels
"""
def plot(self, function, x_range=None, **kwargs):
"""
Plot a function on the coordinate system.
Parameters:
- function: callable, function to plot f(x) -> y
- x_range: tuple, (x_min, x_max, x_step) or None for axes range
- color: curve color
- stroke_width: curve thickness
- discontinuities: list, x-values where function is discontinuous
- dt: float, sampling interval for plotting
Returns:
ParametricCurve
"""
def get_graph(self, func, x_range=None, **kwargs):
"""
Alias for plot() method.
Parameters:
- func: callable, function to plot
- x_range: tuple, domain for plotting
Returns:
ParametricCurve
"""
def input_to_graph_point(self, x, graph):
"""
Convert x-coordinate to point on graph.
Parameters:
- x: float, input value
- graph: ParametricCurve, graph object
Returns:
np.array, point on graph
"""
def point_to_coords(self, point):
"""
Convert scene point to coordinate values.
Parameters:
- point: np.array, point in scene coordinates
Returns:
tuple, (x, y) coordinate values
"""
def coords_to_point(self, x, y):
"""
Convert coordinate values to scene point.
Parameters:
- x: float, x-coordinate
- y: float, y-coordinate
Returns:
np.array, point in scene coordinates
"""
def get_axis_labels(self, x_label="x", y_label="y"):
"""
Create axis labels.
Parameters:
- x_label: str or Mobject, x-axis label
- y_label: str or Mobject, y-axis label
Returns:
VGroup containing labels
"""
def get_axis_label(self, label, axis, edge, direction):
"""
Create single axis label.
Parameters:
- label: str or Mobject, label content
- axis: Line, axis object
- edge: np.array, axis edge for positioning
- direction: np.array, label offset direction
Returns:
Mobject
"""
def add_coordinate_labels(self, x_values=None, y_values=None):
"""
Add coordinate labels to axes.
Parameters:
- x_values: list, x-coordinates to label
- y_values: list, y-coordinates to label
Returns:
VGroup containing all labels
"""
class NumberPlane(Axes):
def __init__(self, x_range=(-8, 8, 1), y_range=(-6, 6, 1), **kwargs):
"""
2D coordinate plane with grid lines.
Parameters:
- x_range: tuple, x-axis range and step
- y_range: tuple, y-axis range and step
- background_line_style: dict, grid line styling
- faded_line_style: dict, secondary grid line styling
- faded_line_ratio: int, ratio of faded to normal lines
- make_smooth_after_applying_functions: bool, smooth after transforms
"""
def get_lines_parallel_to_axis(self, axis, freq=1, **kwargs):
"""
Create grid lines parallel to specified axis.
Parameters:
- axis: Line, reference axis
- freq: float, line frequency
Returns:
VGroup of grid lines
"""
def get_vector(self, coords, **kwargs):
"""
Create vector arrow from origin to coordinates.
Parameters:
- coords: tuple or list, (x, y) coordinates
- **kwargs: Arrow styling parameters
Returns:
Arrow
"""
def prepare_for_nonlinear_transform(self, num_inserted_curves=50):
"""
Prepare plane for nonlinear transformations.
Parameters:
- num_inserted_curves: int, number of interpolation curves
Returns:
NumberPlane (self)
"""Specialized coordinate system for complex number visualization.
class ComplexPlane(NumberPlane):
def __init__(self, **kwargs):
"""
Complex number coordinate plane.
Parameters:
- color: axis and grid color
- unit_size: float, size of unit square
- real_unit_size: float, real axis unit size
- imaginary_unit_size: float, imaginary axis unit size
"""
def number_to_point(self, number):
"""
Convert complex number to scene point.
Parameters:
- number: complex, complex number
Returns:
np.array, corresponding scene point
"""
def point_to_number(self, point):
"""
Convert scene point to complex number.
Parameters:
- point: np.array, scene point
Returns:
complex
"""
def get_default_coordinate_values(self):
"""
Get default coordinate labeling values.
Returns:
tuple, (real_values, imaginary_values)
"""
def add_coordinate_labels(self, **kwargs):
"""
Add complex number coordinate labels.
Returns:
VGroup containing labels
"""Three-dimensional coordinate systems with depth and perspective support.
class ThreeDAxes(Axes):
def __init__(self, x_range=(-6, 6, 1), y_range=(-5, 5, 1), z_range=(-4, 4, 1), **kwargs):
"""
3D Cartesian coordinate axes.
Parameters:
- x_range: tuple, x-axis range and step
- y_range: tuple, y-axis range and step
- z_range: tuple, z-axis range and step
- num_axis_pieces: int, pieces per axis for 3D rendering
- light_source: np.array, 3D lighting direction
"""
def coords_to_point(self, x, y, z):
"""
Convert 3D coordinates to scene point.
Parameters:
- x: float, x-coordinate
- y: float, y-coordinate
- z: float, z-coordinate
Returns:
np.array, scene point with 3D projection
"""
def point_to_coords(self, point):
"""
Convert scene point to 3D coordinates.
Parameters:
- point: np.array, scene point
Returns:
tuple, (x, y, z) coordinates
"""
def get_z_axis_label(self, label="z", **kwargs):
"""
Create z-axis label.
Parameters:
- label: str or Mobject, z-axis label
Returns:
Mobject
"""One-dimensional number lines with customizable ranges and tick marks.
class NumberLine(Line):
def __init__(self, x_range=(-8, 8, 1), **kwargs):
"""
One-dimensional number line.
Parameters:
- x_range: tuple, (min, max, step) for number line
- unit_size: float, distance per unit
- include_ticks: bool, show tick marks
- tick_size: float, tick mark length
- include_numbers: bool, show number labels
- numbers_with_elongated_ticks: list, numbers with longer ticks
- numbers_to_exclude: list, numbers to skip
- label_direction: np.array, direction for number labels
- line_to_number_buff: float, spacing between line and labels
- decimal_number_config: dict, DecimalNumber styling
- exclude_zero_from_default_numbers: bool, hide zero label
"""
def number_to_point(self, number):
"""
Convert number to point on line.
Parameters:
- number: float, number value
Returns:
np.array, corresponding point on line
"""
def point_to_number(self, point):
"""
Convert point to number value.
Parameters:
- point: np.array, point on or near line
Returns:
float, corresponding number
"""
def get_tick(self, x, size=None):
"""
Create tick mark at specified position.
Parameters:
- x: float, position for tick
- size: float, tick length
Returns:
Line, tick mark
"""
def get_tick_marks(self):
"""
Create all tick marks for the number line.
Returns:
VGroup of tick marks
"""
def add_numbers(self, x_values=None, **kwargs):
"""
Add number labels to the line.
Parameters:
- x_values: list, specific numbers to label
Returns:
VGroup of number labels
"""Objects for plotting mathematical functions and parametric curves.
class FunctionGraph(ParametricCurve):
def __init__(self, function, x_range=(-8, 8), **kwargs):
"""
Graph of mathematical function y = f(x).
Parameters:
- function: callable, function to plot f(x) -> y
- x_range: tuple, (x_min, x_max) domain
- color: curve color
- stroke_width: curve thickness
- discontinuities: list, x-values with discontinuities
- dt: float, sampling resolution
"""
class ParametricCurve(VMobject):
def __init__(self, function, t_range=(0, 1), **kwargs):
"""
Parametric curve r(t) = (x(t), y(t), z(t)).
Parameters:
- function: callable, parametric function t -> (x, y, z)
- t_range: tuple, (t_min, t_max) parameter range
- dt: float, parameter sampling step
- discontinuities: list, t-values with discontinuities
"""
def get_point_from_function(self, t):
"""
Get point on curve at parameter value t.
Parameters:
- t: float, parameter value
Returns:
np.array, point on curve
"""
class ImplicitFunction(VMobject):
def __init__(self, func, x_range=(-8, 8), y_range=(-6, 6), **kwargs):
"""
Implicit function curve F(x, y) = 0.
Parameters:
- func: callable, implicit function F(x, y) -> float
- x_range: tuple, x domain for plotting
- y_range: tuple, y domain for plotting
- resolution: int, grid resolution for level finding
- tolerance: float, zero-level tolerance
"""from manimgl import *
class CoordinateExample(Scene):
def construct(self):
# Create coordinate system
axes = Axes(
x_range=(-5, 5, 1),
y_range=(-3, 3, 1),
axis_config={"color": WHITE}
)
# Add labels
labels = axes.get_axis_labels(x_label="x", y_label="y")
self.play(ShowCreation(axes))
self.play(Write(labels))
self.wait()class FunctionExample(Scene):
def construct(self):
axes = Axes(x_range=(-3, 3), y_range=(-2, 2))
# Plot functions
sin_graph = axes.plot(lambda x: np.sin(x), color=BLUE)
cos_graph = axes.plot(lambda x: np.cos(x), color=RED)
# Create labels
sin_label = MathTex("\\sin(x)").set_color(BLUE)
cos_label = MathTex("\\cos(x)").set_color(RED)
self.add(axes)
self.play(ShowCreation(sin_graph))
self.play(ShowCreation(cos_graph))
self.play(Write(sin_label), Write(cos_label))
self.wait()class ThreeDExample(ThreeDScene):
def construct(self):
axes = ThreeDAxes(
x_range=(-3, 3),
y_range=(-3, 3),
z_range=(-2, 2)
)
# Set camera angle
self.set_camera_orientation(phi=75*DEGREES, theta=45*DEGREES)
self.play(ShowCreation(axes))
self.wait()class ComplexExample(Scene):
def construct(self):
plane = ComplexPlane()
# Plot complex numbers
z1 = 2 + 3j
z2 = -1 + 2j
dot1 = Dot(plane.number_to_point(z1), color=BLUE)
dot2 = Dot(plane.number_to_point(z2), color=RED)
label1 = MathTex("2 + 3i").next_to(dot1, UR)
label2 = MathTex("-1 + 2i").next_to(dot2, UL)
self.add(plane)
self.play(ShowCreation(dot1), Write(label1))
self.play(ShowCreation(dot2), Write(label2))
self.wait()class ParametricExample(Scene):
def construct(self):
axes = Axes(x_range=(-3, 3), y_range=(-3, 3))
# Parametric curve (circle)
curve = ParametricCurve(
lambda t: np.array([2*np.cos(t), 2*np.sin(t), 0]),
t_range=(0, TAU),
color=YELLOW
)
self.add(axes)
self.play(ShowCreation(curve))
self.wait()class GraphAnimation(Scene):
def construct(self):
axes = Axes(x_range=(-2, 2), y_range=(-1, 3))
# Animated function parameter
k_tracker = ValueTracker(1)
# Function that depends on k
def get_graph():
k = k_tracker.get_value()
return axes.plot(lambda x: k * x**2, color=BLUE)
graph = always_redraw(get_graph)
self.add(axes, graph)
self.play(k_tracker.animate.set_value(3), run_time=2)
self.play(k_tracker.animate.set_value(0.5), run_time=2)
self.wait()Install with Tessl CLI
npx tessl i tessl/pypi-manimgldocs