pythonsyntaxinversemanim

Python Manim NumberLine Scaling Syntax


Manim NumberLine object:
https://docs.manim.community/en/stable/reference/manim.mobject.graphing.number_line.NumberLine.html
Manim scale object:
https://docs.manim.community/en/stable/_modules/manim/mobject/graphing/scale.html

a = NumberLine(
    x_range=[-2.5, 2.5, 0.5],
    length=13,
    decimal_number_config={"num_decimal_places": 1},
    include_numbers=True,
    font_size = 50,
    tick_size = 0.2,
    scaling = LinearBase.function(lambda x:1/x)
)

I'm a novice coder, so I don't understand how to tell my numberline to scale the x_range, by reciprocating the values in the range.
What's the proper syntax to scale my number line, by inverting all the values in x_range?


Solution

  • https://github.com/ManimCommunity/manim/discussions/2758

    Answered on GitHub by @behackl.

    from typing import Iterable
    
    from manim import *
    from manim.mobject.graphing.scale import _ScaleBase
    
    class InverseScale(_ScaleBase):
        def function(self, value: float) -> float:
            return 1 / value
        
        def inverse_function(self, value: float) -> float:
            return 1 / value
            
        def get_custom_labels(self, val_range: Iterable[float], unit_decimal_places=None) -> Iterable[Mobject]:
            label_array = [MathTex(r"%s" % float(np.around(v,1)), color = "#FF40FF") for v in val_range]
            label_array[20] = MathTex('undef')
    #         label_array = [MathTex('undef') if i == MathTex('inf') else i for i in label_array]
            return label_array
    

    Create a custom class.