数轴是数学中的一个基本概念,它规定了原点、正方向和单位长度的直线。Manim中的NumberLine就是一个专门用来表示数轴的对象,它允许用户设置数轴的范围、间隔和显示长度等参数,从而灵活地在动画中展示数学中的一维数值变化。下面将介绍Manim中的NumberLine对象的基本功能到使用示例。

1. 主要参数

NumberLine的参数很多,定制性很强,能够满足各式各样对于数轴显示的需求。

参数名称 类型 说明
x_range Sequence[float] 数轴刻度的范围
length float 数轴的长度
unit_size float 刻度之间的距离
include_ticks bool 是否包含刻度
tick_size float 刻度标记的长度
numbers_with_elongated_ticks Iterable[float] 拉伸的特殊刻度列表
longer_tick_multiple int 被拉伸刻度的拉伸比例
rotation float 数轴旋转的角度
stroke_width float 数轴线的粗细
include_tip bool 数轴是否包含箭头
tip_width float 箭头的宽度
tip_height float 箭头的高度
tip_shape ArrowTip 数轴的箭头类型
include_numbers bool 刻度是否包含数值
scaling _ScaleBase 数轴的比例
font_size float 刻度上文字的大小
label_direction Sequence[float] 刻度上文字的方向
label_constructor VMobject 构建刻度上文字的对象
line_to_number_buff float 数轴的线和文字的距离
decimal_number_config dict 刻度上文字的样式配置
numbers_to_exclude Iterable[float] 不需要显示文字的刻度列表
numbers_to_include Iterable[float] 需要显示文字的刻度列表

参数很多,但是主要分为3个部分(数轴的线,刻度和数值)的设置。

比如,参数名称中含有 tick的,一般是设置上图中蓝色部分(刻度)的;

参数名称中含有tip或line的,一般是设置上图中红色线以及箭头部分(数轴线)的;

参数名称中含有label或number的,一般是设置上图中黄色数字部分(数值)的。

其他的参数也是与上面3部分相关的,后面的示例会演示各种参数的使用。

2. 主要方法

数轴提供的主要方法有:

名称 说明
add_labels 数轴上某些位置添加标签文字
add_numbers 数轴上添加数值
add_ticks 数轴上添加刻度
get_labels 获取数轴上的标签文字
get_number_mobject 获取数轴上的数值对象
get_tick 获取数轴上的刻度
get_tick_range 获取数轴上的刻度范围
n2p(number_to_point) 根据数值得到此数值在数轴上的位置
p2n(point_to_number) 根据数轴上的位置得到对应的数值

动态在数轴上添加,修改,删除内容时,这些方法很有用。

3. 使用示例

数轴(NumberLine)的参数和方法都不少,无法一一演示,下面的示例从数轴的样式,刻度和数值设置,以及数据点在数轴上动态移动等几个方面演示数轴的使用。

3.1. 数轴大小和颜色

数轴(NumberLine)的大小和颜色是最常设置的,相关的参数主要有length,stroke_width和color。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from manim import *
import numpy as np
def head(cn, en):
t = Text(cn, font="STLiti", font_size=27).to_edge(UP)
s = Text(en, font="Consolas", font_size=20, color=YELLOW).next_to(t, DOWN, buff=0.10)
return VGroup(t, s)
def foot(cn):
return Text(cn, font="STLiti", font_size=20, color=YELLOW).to_edge(DOWN)
class NumberLineLength(Scene):
def construct(self):
self.add(head("NumberLine:length 控制显示长度", "length / unit_size"))
l1 = NumberLine(x_range=[-5, 5, 1], length=2).shift(UP * 1.8)
l2 = NumberLine(x_range=[-5, 5, 1], length=5).shift(UP * 0.1)
l3 = NumberLine(x_range=[-5, 5, 1], length=8).shift(DOWN * 1.7)
l2.add_numbers(font_size=20)
l3.add_numbers(font_size=20)
self.add(Text("length = 2", font="Consolas", font_size=19, color=GREY_A).next_to(l1, LEFT))
self.add(Text("length = 5", font="Consolas", font_size=19, color=GREY_A).next_to(l2, LEFT))
self.add(Text("length = 8", font="Consolas", font_size=19, color=GREY_A).next_to(l3, LEFT))
self.play(LaggedStart(Create(l1), Create(l2), Create(l3), lag_ratio=0.35), run_time=3.4)
self.play(FadeIn(foot("x_range 相同,length 越大刻度越稀疏展开")), run_time=0.4)
self.wait(1.8)

下载 Python 脚本

3.2. 数轴线的箭头

数轴默认是没有箭头的,通过tip相关参数,可以在数轴的正方向添加不同样式箭头。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from manim import *
import numpy as np
def head(cn, en):
t = Text(cn, font="STLiti", font_size=27).to_edge(UP)
s = Text(en, font="Consolas", font_size=20, color=YELLOW).next_to(t, DOWN, buff=0.10)
return VGroup(t, s)
def foot(cn):
return Text(cn, font="STLiti", font_size=20, color=YELLOW).to_edge(DOWN)
class NumberLineTip(Scene):
def construct(self):
self.add(head("NumberLine:箭头与端点样式", "include_tip / tip_shape"))
l1 = NumberLine(x_range=[-5, 6, 1], include_tip=True,
tip_width=0.15, tip_height=0.15).shift(UP * 1.5)
l2 = NumberLine(x_range=[-5, 6, 1], include_tip=True,
tip_shape=ArrowCircleTip).shift(DOWN * 1.5)
l1.add_numbers(font_size=22)
l2.add_numbers(font_size=22)
self.add(Text("默认三角箭头", font="STLiti", font_size=19, color=GREY_A).next_to(l1, UP))
self.add(Text("ArrowCircleTip", font="Consolas", font_size=19, color=GREY_A).next_to(l2, UP))
self.play(Create(l1), run_time=2.0)
self.play(Create(l2), run_time=2.0)
self.play(FadeIn(foot("tip_shape 可换成任意 ArrowTip 子类")), run_time=0.4)
self.wait(1.6)

下载 Python 脚本

3.3. 刻度和数值

默认情况下,数轴上的刻度和数值是依次显示的,而通过x_range参数,可以设置数值间隔显示;我们可以通过scaling参数,可以在相同间隔的刻度上显示不均匀的数值;还可以突出显示某些刻度。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from manim import *
import numpy as np
def head(cn, en):
t = Text(cn, font="STLiti", font_size=27).to_edge(UP)
s = Text(en, font="Consolas", font_size=20, color=YELLOW).next_to(t, DOWN, buff=0.10)
return VGroup(t, s)
def foot(cn):
return Text(cn, font="STLiti", font_size=20, color=YELLOW).to_edge(DOWN)
class NumberLineStep(Scene):
def construct(self):
self.add(head("NumberLine:x_range 步长与刻度", "x_range=[start, end, step]"))
l1 = NumberLine(x_range=[-6, 6, 1], length=9).shift(UP * 1.5)
l2 = NumberLine(x_range=[-6, 6, 2], length=9).shift(DOWN * 1.5)
l1.add_numbers(font_size=20)
l2.add_numbers(font_size=20)
self.add(Text("step = 1", font="Consolas", font_size=19, color=GREY_A).next_to(l1, LEFT))
self.add(Text("step = 2", font="Consolas", font_size=19, color=GREY_A).next_to(l2, LEFT))
self.play(Create(l1), run_time=2.2)
self.play(Create(l2), run_time=2.2)
self.play(FadeIn(foot("第三个分量决定主刻度间隔,也决定 add_numbers 的标注")), run_time=0.4)
self.wait(1.4)

下载 Python 脚本

3.4. 数轴上的数据点

数轴上的点和显示在屏幕的位置是不一样的,通过数轴提供的函数n2p和p2n,可以很容易的将屏幕上的点映射到数轴上,也可以根据数轴上的点计算其在屏幕上的位置。下面的动画示例,首先根据数轴上的值获取其在屏幕上的位置,然后绘制这个点。接下来,移动这个点,移动的过程中同时显示此点在数轴上的数值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from manim import *
import numpy as np
def head(cn, en):
t = Text(cn, font="STLiti", font_size=27).to_edge(UP)
s = Text(en, font="Consolas", font_size=20, color=YELLOW).next_to(t, DOWN, buff=0.10)
return VGroup(t, s)
def foot(cn):
return Text(cn, font="STLiti", font_size=20, color=YELLOW).to_edge(DOWN)
class NumberLineN2P(Scene):
def construct(self):
self.add(head("NumberLine:n2p 与 p2n 互转", "add_updater / ValueTracker"))
line = NumberLine(x_range=[-6, 6, 1], length=10).shift(DOWN * 0.2)
line.add_numbers(font_size=20)
x = ValueTracker(-4.0)
dot = always_redraw(lambda: Dot(line.n2p(x.get_value()), color=YELLOW, radius=0.14))
txt = always_redraw(lambda: Text("x = %.2f" % line.p2n(dot.get_center()),
font="Consolas", font_size=26, color=YELLOW).to_edge(UP).shift(DOWN * 1.0))
self.add(line, dot, txt)
self.play(x.animate.set_value(4.0), run_time=3.6, rate_func=there_and_back)
self.play(FadeIn(foot("n2p 把数值转坐标,p2n 把坐标读回数值")), run_time=0.4)
self.wait(1.9)

下载 Python 脚本

好了,今天的教程就到这里。