Location>code7788 >text

manim learning by doing - axes

Popularity:664 ℃/2024-10-16 13:31:04

number lineIt is a fundamental concept in mathematics that specifies the origin, the positive direction, and the unit length of a straight line.

Manimhit the nail on the headNumberLineIt is an object dedicated to representing the numerical axis, which allows the user to set parameters such as the range, interval and display length of the numerical axis, so as to flexibly display one-dimensional numerical changes in mathematics in the animation.

As described belowManimhit the nail on the headNumberLineobjects from basic functions to usage examples.

1. Main parameters

NumberLineA wide range of parameters and customizability are available to meet a wide variety of needs for axis displays.

Parameter name typology clarification
x_range Sequence[float] Range of numerical axis scales
length float Length of the axes
unit_size float Distance between scales
include_ticks bool Scale included or not
tick_size float Length of the scale mark
numbers_with_elongated_ticks Iterable[float] List of special scales for stretching
longer_tick_multiple int Stretch ratio of the stretched scale
rotation float Angle of rotation of the numerical axis
stroke_width float Thickness of the numbered axis lines
include_tip bool Whether the axis contains arrows
tip_width float Width of arrows
tip_height float Height of arrows
tip_shape ArrowTip Types of arrows for numeric axes
include_numbers bool Whether the scale contains a numerical value
scaling _ScaleBase Scaling of axes
font_size float Size of the text on the scale
label_direction Sequence[float] Orientation of the text on the scale
label_constructor VMobject Objects that build text on a scale
line_to_number_buff float Distance between the line of the numerical axis and the text
decimal_number_config dict Stylized configuration of text on the scale
numbers_to_exclude Iterable[float] Scale lists that do not require text to be displayed
numbers_to_include Iterable[float] List of scales for which text needs to be displayed

There are many parameters, but they are divided into 3 main parts (the axis of thethreadsgraduated scalecap (a poem)numerical value) settings.

For example, a parameter name containingticks, which are generally set to the blue part (scale) in the picture above;

The parameter name contains thetipmaybelines, generally set the red line in the picture above as well as the arrowed portion (the numbered axis line);

The parameter name contains thelabelmaybenumbers, which generally set the yellow numbered portion (numerical value) in the above figure.

Other parameters are also relevant to the 3 sections above, and later examples will demonstrate the use of the various parameters.

2. Main approaches

The main methods provided by the numbered axes are:

name (of a thing) clarification
add_labels Adding Label Text to Certain Positions on the Axis
add_numbers Adding Values to the Axis
add_ticks Adding a Scale to the Numeric Axis
get_labels Get the label text on the axis
get_number_mobject Getting numeric objects on an axis
get_tick Getting the scale on an axis
get_tick_range Get the scale range on the numeric axis
n2p(number_to_point) Get the position of this value on the numerical axis based on the value
p2n(point_to_number) Get the corresponding value based on the position on the number axis

These methods are useful when dynamically adding, modifying, and deleting content on an axis.

3. Examples of use

NumberLine (NumberLine) parameters and methods are quite a lot, can not be demonstrated one by one, the following example from the style of the number axis, the

Scale and value settings, and dynamic movement of data points on the axes are demonstrated in several ways.

3.1 Axis size and color

number lineNumberLine(used for emphasis)adults and childrencap (a poem)coloris the most commonly set, the relevant parameters are mainlylengthstroke_widthcap (a poem)color

# Number lines with different lengths
l1 = NumberLine(
    x_range=[-5, 5], length=2, l1 = NumberLine(
    length=2, )
)
l2 = NumberLine(
    x_range=[-5, 5], length=4, )
    length=4, )
)
l3 = NumberLine(
    x_range=[-5, 5], length=6, )
    length=6, )
)

# Thick, thick, and colorful number line
l4 = NumberLine(
    x_range=[-5, 5], stroke_width=1, x_range=[-5, 5], l4 = NumberLine(
    stroke_width=1, color=BLUE,
    color=BLUE, )
)
l5 = NumberLine(
    x_range=[-5, 5], stroke_width=3, color=BLUE, )
    stroke_width=3, color=RED, )
    color=RED, )
)
l6 = NumberLine(
    x_range=[-5, 5], stroke_width=6, color=RED, )
    stroke_width=6, color=GREEN, )
    color=GREEN, )
)

3.2 Arrowheads for numbered axes

The numeric axes do not have arrows by default, and they can be changed with thetipA related parameter that adds different styles of arrows in the positive direction of the axis.

l1 = NumberLine(
    x_range=[-5, 6],
    include_tip=True,
    tip_width=0.15,
    tip_height=0.15,
)
l2 = NumberLine(
    x_range=[-5, 6],
    include_tip=True,
    tip_shape=ArrowCircleTip,
    tip_width=0.15,
    tip_height=0.15,
)
l3 = NumberLine(
    x_range=[-5, 6],
    include_tip=True,
    tip_shape=ArrowCircleFilledTip,
    tip_width=0.15,
    tip_height=0.15,
)

l4 = NumberLine(
    x_range=[-5, 6],
    include_tip=True,
    tip_shape=ArrowSquareTip,
    tip_width=0.15,
    tip_height=0.15,
)
l5 = NumberLine(
    x_range=[-5, 6],
    include_tip=True,
    tip_shape=StealthTip,
    tip_width=0.15,
    tip_height=0.15,
)

3.3 Scale and value

By default, the number axis on thegraduated scalecap (a poem)numerical valueis displayed sequentially, while the display via thex_rangeparameter, you can set the value interval display;

pass (a bill or inspection etc)scalingparameter, it is possible to display uneven values on equally spaced scales; it is also possible to highlight certain scales.

# Default number line
l1 = NumberLine(
    x_range=[-6, 6, 1], )
)

# Number line for numeric intervals
l2 = NumberLine(
    x_range=[-6, 6, 2], )
)

# Number is an exponent with a base of 10
l3 = NumberLine(
    x_range=[0, 5, 1],
    scaling=LogBase(base=10), )
)

# Highlight certain scales
l4 = NumberLine(
    x_range=[-6, 6, 1], )
)
[7].set_color(RED).scale(2)
[3].set_color(GREEN).scale(2)

3.4 Data points on the axes

The points on the axes are not the same as the positions displayed on the screen, through the functions provided by the axesn2pcap (a poem)p2n

It is easy to map points on the screen to the axes and also to calculate their position on the screen based on the points on the axes.

The following animation example first gets its position on the screen based on the value on the axis and then draws the point.

Next, move this point, displaying the value of this point on the numeric axis as you move.

l = NumberLine(
    x_range = [-6, 6, 1], )
)

# n2p Get the position of -4 on the screen
p = l.n2p(-4)
d = Dot(p, color=BLUE)

# initial value
txt = MathTex("-4.00")

# p2n Get the value on the numeric axis
txt.add_updater(
    lambda x: (
        MathTex(
            round(l.p2n(d.get_center()), 2),
            color=RED,
            color=RED, font_size=30,
        ).next_to(d, UP, buff=0.2)
    )
)

# Move this point to see the value change
(.move_to(RIGHT))

4. Neighborhood

The complete code for the article is on a web disk (number_line.py),

Download at.Full Code (Access code: 6872)