pythonmanim

Manim Community: x-axis in radians


I'm trying to change the x-axis in my Manim project to radians, while leaving the y-axis unchanged:

from manim import *
import numpy as np


class RadiansTest(Scene):
    def construct(self):
        axes = Axes(
            x_range=[-10, 10, PI/2],
            y_range=[-10, 10, 1],
            x_length=10,
            y_length=10,
            axis_config={"include_numbers": True,
                         "include_ticks": True}
        )

        graph = axes.plot(np.sin, color=BLUE, x_range=[-10, 10])

        self.play(Create(axes), Create(graph))
        self.wait(2)

I already set the tick distance to pi/2, now I'd like to have the ticks labeled using Latex.

I tried to add this to my code, and although it didn't throw any errors it also didn't change anything:

        tick_labels = {
            -2 * PI: r"$-2\pi$",
            -3 * PI / 2: r"$-\frac{3\pi}{2}$",
            -PI: r"$-\pi$",
            -PI / 2: r"$-\frac{\pi}{2}$",
            0: r"$0$",
            PI / 2: r"$\frac{\pi}{2}$",
            PI: r"$\pi$",
            3 * PI / 2: r"$\frac{3\pi}{2}$",
            2 * PI: r"$2\pi$",
        }

        # Set custom tick labels for the x-axis
        axes.x_axis.set_tick_labels(tick_labels)

Thanks!


Solution

  • You have to add a self.wait() add the end s.t. your changes can appear. But you will see that your approach generates a massive amount of digits.

    Here is an approach which is not perfect but shows how it can be done in general:

        axes = Axes(
            x_range=[-10, 10, 3.14/2],
            y_range=[-10, 10, 1],
            x_length=10,
            y_length=10,
            axis_config={"include_numbers": True, "include_ticks": True},
            x_axis_config={
                "include_numbers": False,
            },
        )
    
        ...
    
        values_x = [
            (-2 * PI, r"$-2\pi$"),
            (-3 * PI / 2, r"$-\frac{3\pi}{2}$"),
            (-PI, r"$-\pi$"),
            (-PI / 2, r"$-\frac{\pi}{2}$"),
            (0, r"$0$"),
            (PI / 2, r"$\frac{\pi}{2}$"),
            (PI, r"$\pi$"),
            (3 * PI / 2, r"$\frac{3\pi}{2}$"),
            (2 * PI, r"$2\pi$"),
        ]
        x_axis_labels = VGroup()  # Create a group named x_axis_labels
        #   pos.   tex.
        for x_val, x_tex in values_x:
            tex = Tex(x_tex)  # Convert string to tex
            tex.next_to(axes.coords_to_point(x_val, 0), DOWN)  # Put tex on the position
            x_axis_labels.add(tex)  # Add tex in graph
            
        self.add(x_axis_labels)
        self.wait()
    

    I adapted the answer from here: In manim, how do you set the text of the axis labels different to the axis units?