使用 Manim 绘制自定义函数曲线


你是否曾经想过,让复杂的函数曲线在屏幕上以动画形式优雅地呈现出来?或者希望通过编程,自定义并控制数学图形的每一个细节,使其生动地表达抽象的概念?本文将带你探索如何使用 Manim 实现这一切。

一、Manim 简介

Manim(Mathematical Animation Engine)是一个功能强大的Python库,用于创建专业级的数学动画。由知名数学视频博主3Blue1Brown(Grant Sanderson)开发,Manim能够以编程方式生成精美的动画效果,从函数曲线到几何图形,再到复杂的动画演示,皆可轻松实现。

二、Axes 类概述

绘制自定义函数曲线会到Axes类,它是Manim中用于创建二维坐标轴的对象。继承自CoordinateSystem,并提供了丰富的方法来定制坐标轴的外观和功能。

Axes 类的主要功能包括:

  • 创建自定义范围和刻度的坐标轴
  • 绘制函数曲线和参数方程
  • 添加坐标网格和刻度线
  • 在坐标轴上添加标签和注释

三、创建坐标轴

3.1坐标轴

要创建一个坐标轴,首先需要实例化Axes类。以下是基本的使用方法

1
axes = Axes()

效果:

3.2坐标范围和刻度设置

x_range:设置x轴的范围和刻度,格式:

1
x_range=[x_min, x_max, x_step]

例如:

1
x_range=[-5, 5, 1]

表示x轴从-5到5,刻度间隔为1,同样的,

1
y_range=[x_min, x_max, x_step]

例如

1
y_range=[-5, 5, 1]

表示y轴从-5到5,刻度间隔为1,理解了基础代码,我们就可以设置坐标系基本样式,示例:

1
2
3
4
axes = Axes(
x_range=[0, 6, 1],
y_range=[-4, 4, 1],
)

3.3轴标签和刻度数字

axis_config:用于定制轴的样式,一个字典,设置轴的通用配置。常用的配置项:

  • include_numbers:是否在刻度上显示数字(默认为 False)。
  • numbers_to_include:指定要显示数字的列表。
  • numbers_with_elongated_ticks:指定要加长刻度线的数字列表。

示例:在刻度上显示数字

1
2
3
4
5
axes = Axes(
x_range=[-6, 6, 1],
y_range=[-4, 4, 1],
axis_config={"include_numbers": True},
)

3.4更高级的配置

如果需要更精细地控制轴的配置,可以使用 x_axis_configy_axis_config

1
2
3
4
5
6
7
8
9
10
11
12
13
14
axes = Axes(
x_range=[-6, 6, 1],
y_range=[-4, 4, 1],
x_axis_config={
"include_numbers": True,
"numbers_to_include": [-5, 0, 5],
"numbers_with_elongated_ticks": [-5, 0, 5],
},
y_axis_config={
"include_numbers": True,
"numbers_to_include": [-3, 0, 3],
"numbers_with_elongated_ticks": [-3, 0, 3],
},
)

3.5其中的配置项:

  • include_numbers:是否在刻度上显示数字(默认为 False)。
  • numbers_to_include:指定要显示数字的列表。
  • numbers_with_elongated_ticks:指定要加长刻度线的数字列表。

3.6轴样式定制

可以通过 axis_configx_axis_configy_axis_config 来定制轴的样式。

  • stroke_color:轴的颜色。
  • stroke_width:轴的线宽。
  • include_ticks:是否显示刻度线。
  • tick_size:刻度线的长度。
  • include_tip:是否在轴的末端显示箭头。
  • tip_length:箭头的长度。

示例:定制轴的颜色和箭头

1
2
3
4
5
6
7
8
9
10
11
12
13
14
axes = Axes(
x_range=[-6, 6, 1],
y_range=[-4, 4, 1],
x_axis_config={
"stroke_color": RED,
"include_tip": True,
"tip_length": 0.2,
},
y_axis_config={
"stroke_color": GREEN,
"include_tip": True,
"tip_length": 0.2,
},
)

四、在坐标轴上绘制函数

4.1使用 plot 方法绘制函数曲线

Axes 提供了 plot 方法,用于在坐标轴上绘制函数曲线。

1
curve = axes.plot(lambda x: x**2, color=BLUE)
  • lambda x: x**2:要绘制的函数,可以是任何可计算的 Python 函数。
  • color:曲线的颜色。

4.2自定义函数的绘制

您可以绘制任何自定义函数,例如三角函数、指数函数等。

1
curve = axes.plot(lambda x: np.sin(x), color=YELLOW)

示例:绘制自定义函数

1
2
3
def custom_function(x):
return np.exp(-x**2)
curve = axes.plot(custom_function, color=PURPLE)

4.3曲线样式定制

可以通过以下参数定制曲线的样式:

x_range:指定函数曲线的 x 范围,格式为 [x_min, x_max]。

use_smoothing:是否对曲线进行平滑处理(默认 True)。

stroke_width:曲线的线宽。

示例:定制曲线范围和线宽

1
2
3
4
5
6
curve = axes.plot(
lambda x: np.log(x),
x_range=[0.1, 5],
color=ORANGE,
stroke_width=4,
)

五、添加图形元素

5.1添加函数标签

为了在曲线上添加标签,可以使用 get_graph_label 方法。

1
2
3
4
5
6
label = axes.get_graph_label(
curve,
label="y = x^2",
x_val=2,
direction=UP,
)

curve:要添加标签的曲线对象。

label:标签内容,可以是字符串或 MathTex 对象。

x_val:标签在曲线上对应的 x 值位置。

direction:标签相对于曲线的方向。

buff:标签与曲线之间的距离。

5.2添加点、线和注释

添加点

1
dot = Dot(axes.c2p(2, 4), color=RED)

axes.c2p(x, y):将数据坐标转换为场景坐标。

添加垂直线

1
v_line = axes.get_vertical_line(axes.c2p(2, 4), color=GREEN)

添加水平线

1
h_line = axes.get_horizontal_line(axes.c2p(2, 4), color=GREEN)

添加注释文本

1
text = Text("关键点", font_size=24).next_to(dot, RIGHT)

六、示例:

6.1上述功能完整示例

下面是一个完整的示例,演示如何使用 Axes 绘制自定义函数曲线,并添加各种元素。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from manim import *

class CustomFunctionPlot(Scene):
def construct(self):
# 创建坐标轴
axes = Axes(
x_range=[-5, 5, 1],
y_range=[-1, 1, 0.2],
x_length=10,
y_length=6,
axis_config={
"include_numbers": True,
"include_tip": True,
"tip_length": 0.2,
},
)

# 绘制函数曲线
func = lambda x: np.sin(x)
curve = axes.plot(func, color=BLUE)

# 添加函数标签
label = axes.get_graph_label(
curve,
label="y = \\sin(x)",
x_val=2,
direction=UR,
buff=0.5,
)

# 添加垂直线和点
x_val = PI / 2
y_val = func(x_val)
dot = Dot(axes.c2p(x_val, y_val), color=RED)
v_line = axes.get_vertical_line(axes.c2p(x_val, y_val), color=YELLOW)

# 添加注释
text = MathTex("x = \\frac{\\pi}{2}").next_to(dot, DR)

# 创建动画
self.play(Create(axes))
self.play(Create(curve), Write(label))
self.play(Create(v_line), FadeIn(dot), Write(text))
self.wait(2)

运行效果

坐标轴:从 -55 的 x 轴和从 -11 的 y 轴,带有数字和箭头。

函数曲线:绘制了正弦函数 y = sin(x) 的曲线,颜色为蓝色。

函数标签:在 x=2 的位置,曲线的右上方添加了函数的数学表达式标签。

垂直线和点:在 x = π/2 处添加了一个红色的点和一条黄色的垂直线。

注释:在点的右下方添加了注释 x = π/2

6.2示例: Sigmoid函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
%%manim -qm SigmoidFunctionPlot

class SigmoidFunctionPlot(Scene):
def construct(self):
axes = Axes(
x_range=[-7, 7, 1],
y_range=[0, 1, 0.1],
x_length=16,
y_length=6,
tips=False,
axis_config={"include_numbers": True},
)
sigmoid_graph = axes.plot(
lambda x: 1 / (1 + np.exp(-x)),
color=BLUE,
)
sigmoid_label = axes.get_graph_label(
sigmoid_graph, label="\\sigma(x) = \\frac{1}{1 + e^{-x}}", x_val=4, direction=DOWN,buff=0.5
)
self.play(Create(axes))
self.play(Create(sigmoid_graph), Write(sigmoid_label))
self.wait(2)

效果

七、总结

使用ManimAxes类来绘制自定义函数曲线关键点:

  1. 创建坐标轴:使用 Axes 类,并设置 x_rangey_range 等参数。
  2. 定制坐标轴:通过 axis_configx_axis_configy_axis_config 定制轴的外观和功能。
  3. 绘制函数曲线:使用 plot 方法,将自定义函数绘制在坐标轴上。
  4. 添加图形元素:利用 get_graph_labelDotget_vertical_line 等方法添加标签、点和线。
  5. 动画效果:使用 CreateWriteFadeIn 等方法为元素添加动画。

本文转自 https://www.cnblogs.com/ghj1976/p/18469840/shi-yong-manim-hui-zhi-zi-ding-yi-han-shu-qu-xian,如有侵权,请联系删除。