pythonmathgraph-visualizationmanimalgorithm-animation

Manim axes alignment problem: How to align ParametricFunction and Points with Axes?


How do I align my ParametricFunction and Points with Axes?

I felt inspired to build parametric animations in manim. I have played around with the package, gotten a variety of 2D and parametric curves rendering, and am generally enjoying it. However, I cannot seem to get the axes aligned with my parametric equations, plotted using ParametricFunction in a ThreeDScene. Technically I am using ThreeDAxes, but I have tried this with Axes as well and I get a similar result.

The ParametricFunction of y = (x-2)^2-1 is plotted in BLUE. For comparison, I decided to plot y = (x-2)^2-1 using axes.plot in RED. The RED version is correctly aligned with the axes. The BLUE parametric version is not.

I also plotted 2 yellow points: the y-intercept at (0,3), and the global minima at (2, -1). They're tiny, but you can see they they align with the BLUE ParametricFunction if you look closely.

y=(x-2)^2-1 using ThreeDAxes y=(x-2)^2-1 using ThreeDAxes

y=(x-2)^2-1 using Axes y=(x-2)^2-1 using Axes

alignment_test.py

from manim import (
    Axes,
    ThreeDAxes,
    ThreeDScene,
    ParametricFunction,
    Point,
    BLUE,
    RED,
    YELLOW,
)
from manim.utils.file_ops import open_file as open_media_file
import numpy as np


class ParabolaAlignmentTest(ThreeDScene):

    def construct(self):
        # axes = Axes()
        axes = ThreeDAxes()
        self.add(axes)
        self.add(axes.plot(lambda t: (t - 2) ** 2 - 1, x_range=[-1, 5], color=RED))
        self.add(
            ParametricFunction(
                lambda t: np.array([t, (t - 2) ** 2 - 1, 0]),
                t_range=[-1, 5],
                color=BLUE,
            )
        )
        self.add(Point([0, 3, 0], color=YELLOW))
        self.add(Point([2, -1, 0], color=YELLOW))
        self.wait(1)


if __name__ == "__main__":
    scene = ParabolaAlignmentTest()
    scene.render()

    # Now, open the .mp4 file!
    open_media_file(scene.renderer.file_writer.movie_file_path)

You can ignore the if __name__ == "__main__": block at the bottom if you are running manim with manim -pql alignment_test.py.


Solution

  • The best solution was pointed out by a comment from CodingWithMagga. Use axes.plot() or axes.plot_parametric_curve().

    It is possible to align ParametricFunction with the axes, after some digging into axes (it internally uses ParametricFunction, with some additional parameters). But the easier way - and the way I would recommend for other users encountering this - is to use the plotting methods attached to axes in order to implicitly have those transformation applied. That keeps everything in alignment.